<!DOCTYPE html>
<!-- saved from url=(0104)https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-5-0 -->
<html class="hasSidebar hasPageActions hasBreadcrumb conceptual has-default-focus js-focus-visible theme-light mathjax-complete" lang="zh-cn" dir="ltr" data-css-variable-support="true" data-authenticated="false" data-auth-status-determined="true" data-target="docs" x-ms-format-detection="none" data-js-focus-visible=""><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta property="og:title" content="如何使用 C# 对 JSON 进行序列化和反序列化 - .NET">
	<meta property="og:type" content="website">
	<meta property="og:url" content="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-how-to">
			<meta property="og:description" content="了解如何使用 System.Text.Json 命名空间在 .NET 中向/从 JSON 进行序列化和反序列化。 包含示例代码。">
		<meta property="og:image" content="https://docs.microsoft.com/en-us/media/logos/logo-ms-social.png">
		<meta property="og:image:alt" content="Microsoft Logo">

	<meta name="twitter:card" content="summary">
	<meta name="twitter:site" content="@docsmsft">

	<meta name="color-scheme" content="light">


	<meta name="apiPlatform" content="dotnet">
<meta name="author" content="tdykstra">
<meta name="bilingual_type" content="hover over">
<meta name="breadcrumb_path" content="/dotnet/breadcrumb/toc.json">
<meta name="depot_name" content="VS.core-docs">
<meta name="description" content="了解如何使用 System.Text.Json 命名空间在 .NET 中向/从 JSON 进行序列化和反序列化。 包含示例代码。">
<meta name="document_id" content="588ae32f-3429-143f-824c-5ab2d046d24a">
<meta name="document_version_independent_id" content="002f9f2a-48d1-0671-a079-b99b9f6bab58">
<meta name="enable_loc_sxs" content="true">
<meta name="gitcommit" content="https://github.com/dotnet/docs.zh-cn/blob/565801fa072fa2c84386c2f1c23966f960a4043c/docs/standard/serialization/system-text-json-how-to.md">
<meta name="locale" content="zh-cn">
<meta name="ms.author" content="tdykstra">
<meta name="ms.contentlocale" content="zh-CN">
<meta name="ms.custom" content="contperfq2">
<meta name="ms.date" content="12/02/2020">
<meta name="ms.devlang" content="dotnet">
<meta name="ms.lasthandoff" content="12/10/2020">
<meta name="ms.locfileid" content="97008833">
<meta name="ms.openlocfilehash" content="1ea4ff71b9e21bd7c5b12598581b33e1e96ebb19">
<meta name="ms.prod" content="dotnet-fundamentals">
<meta name="ms.sourcegitcommit" content="81f1bba2c97a67b5ca76bcc57b37333ffca60c7b">
<meta name="ms.topic" content="how-to">
<meta name="ms.translationtype" content="HT">
<meta name="original_content_git_url" content="https://github.com/dotnet/docs.zh-cn/blob/live-sxs/docs/standard/serialization/system-text-json-how-to.md">
<meta name="search.ms_docsetname" content="core-docs">
<meta name="search.ms_product" content="VS">
<meta name="search.ms_sitename" content="Docs">
<meta name="show_latex" content="true">
<meta name="site_name" content="Docs">
<meta name="uhfHeaderId" content="MSDocsHeader-DotNet">
<meta name="updated_at" content="2020-12-14 12:18 PM">
<meta name="zone_pivot_groups" content="dotnet-version">
<meta name="page_type" content="conceptual">
<meta name="toc_rel" content="../../fundamentals/toc.json">
<meta name="pdf_url_template" content="https://docs.microsoft.com/pdfstore/zh-cn/VS.core-docs/{branchName}{pdfName}">
<meta name="search.mshattr.devlang" content="csharp">
<meta name="word_count" content="4144">


	<meta name="scope" content=".NET">
<link href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-how-to" rel="canonical">
	<title>如何使用 C# 对 JSON 进行序列化和反序列化 - .NET | Microsoft Docs</title>

		<link rel="stylesheet" href="./如何使用 C# 对 JSON 进行序列化和反序列化 - .NET _ Microsoft Docs_files/97269d6d.site-ltr.css">

	<link rel="stylesheet" href="./如何使用 C# 对 JSON 进行序列化和反序列化 - .NET _ Microsoft Docs_files/a96de1e1.conceptual.css">


	<script async="" defer="" crossorigin="anonymous" src="./如何使用 C# 对 JSON 进行序列化和反序列化 - .NET _ Microsoft Docs_files/lux.js.下载"></script><script async="" src="./如何使用 C# 对 JSON 进行序列化和反序列化 - .NET _ Microsoft Docs_files/analytics.js.下载"></script><script id="msdocs-script">
	var msDocs = {
		data: {
			timeOrigin: Date.now(),
			contentLocale: 'zh-cn',
			contentDir: 'ltr',
			userLocale: 'zh-cn',
			userDir: 'ltr',
			pageTemplate: 'Conceptual',
			brand: '',
			context: {

			},
			hasBinaryRating: true,
			hasGithubIssues: true,
			showFeedbackReport: false,
			enableTutorialFeedback: false,
			feedbackSystem: 'GitHub',
			feedbackGitHubRepo: 'dotnet/docs.zh-cn',
			feedbackProductUrl: 'https://aka.ms/feedback/report?space=61',
			contentGitUrl: 'https://github.com/dotnet/docs.zh-cn/blob/live/docs/standard/serialization/system-text-json-how-to.md',
			extendBreadcrumb: false,
			isEditDisplayable: true,
			hideViewSource: false,
			hasPageActions: true,
			hasBookmark: true,
			hasShare: true,
			hasRecommendations: false,
			contributors: [
						{ name: "tdykstra", url: "https://github.com/tdykstra" },
						{ name: "olprod", url: "https://github.com/olprod" }
],
		},
		functions:{}
	};
	</script>
	<script src="./如何使用 C# 对 JSON 进行序列化和反序列化 - .NET _ Microsoft Docs_files/wcp-consent.js.下载"></script>
	<script src="./如何使用 C# 对 JSON 进行序列化和反序列化 - .NET _ Microsoft Docs_files/jsll-4.js.下载"></script>
	<script>window.jsllAwa = window.awa;</script>
	<script nomodule="" src="./如何使用 C# 对 JSON 进行序列化和反序列化 - .NET _ Microsoft Docs_files/bluebird.min.js.下载" integrity="sha384-aD4BDeDGeLXLpPK4yKeqtZQa9dv4a/7mQ+4L5vwshIYH1Mc2BrXvHd32iHzYCQy5" crossorigin="anonymous"></script>
	<script nomodule="" src="./如何使用 C# 对 JSON 进行序列化和反序列化 - .NET _ Microsoft Docs_files/fetch.umd.min.js.下载" integrity="sha384-EQIXrC5K2+7X8nGgLkB995I0/6jfAvvyG1ieZ+WYGxgJHFMD/alsG9fSDWvzb5Y1" crossorigin="anonymous"></script>
	<script nomodule="" src="./如何使用 C# 对 JSON 进行序列化和反序列化 - .NET _ Microsoft Docs_files/template.min.js.下载" integrity="sha384-1zKzI6ldTVHMU7n0W2HpE/lhHI+UG4D9IIaxbj3kT2UhCWicdTuJkTtnKuu0CQzN" crossorigin="anonymous"></script>
	<script nomodule="" src="./如何使用 C# 对 JSON 进行序列化和反序列化 - .NET _ Microsoft Docs_files/url.min.js.下载" integrity="sha384-vn7xBMtpSTfzaTRWxj0kVq0UcsbBrTOgZ/M1ISHqe1V358elYva+lfiEC+T8jLPc" crossorigin="anonymous"></script>
	<script src="./如何使用 C# 对 JSON 进行序列化和反序列化 - .NET _ Microsoft Docs_files/f0a172e9.index-polyfills.js.下载"></script>
		<script src="./如何使用 C# 对 JSON 进行序列化和反序列化 - .NET _ Microsoft Docs_files/qrious.min.js.下载" integrity="sha384-h/SmU/qxwYnRlzklg9PKWpDwyma9UmKuOB//Pz6Cov5KkEqcaFF0vod43/XhoeyJ" crossorigin="anonymous"></script>

		<script src="./如何使用 C# 对 JSON 进行序列化和反序列化 - .NET _ Microsoft Docs_files/d7c98576.index-docs.js.下载"></script><style></style>
<script async="" src="./如何使用 C# 对 JSON 进行序列化和反序列化 - .NET _ Microsoft Docs_files/t.js.下载"></script><style type="text/css">.MathJax_Hover_Frame {border-radius: .25em; -webkit-border-radius: .25em; -moz-border-radius: .25em; -khtml-border-radius: .25em; box-shadow: 0px 0px 15px #83A; -webkit-box-shadow: 0px 0px 15px #83A; -moz-box-shadow: 0px 0px 15px #83A; -khtml-box-shadow: 0px 0px 15px #83A; border: 1px solid #A6D ! important; display: inline-block; position: absolute}
.MathJax_Menu_Button .MathJax_Hover_Arrow {position: absolute; cursor: pointer; display: inline-block; border: 2px solid #AAA; border-radius: 4px; -webkit-border-radius: 4px; -moz-border-radius: 4px; -khtml-border-radius: 4px; font-family: 'Courier New',Courier; font-size: 9px; color: #F0F0F0}
.MathJax_Menu_Button .MathJax_Hover_Arrow span {display: block; background-color: #AAA; border: 1px solid; border-radius: 3px; line-height: 0; padding: 4px}
.MathJax_Hover_Arrow:hover {color: white!important; border: 2px solid #CCC!important}
.MathJax_Hover_Arrow:hover span {background-color: #CCC!important}
</style><style type="text/css">#MathJax_About {position: fixed; left: 50%; width: auto; text-align: center; border: 3px outset; padding: 1em 2em; background-color: #DDDDDD; color: black; cursor: default; font-family: message-box; font-size: 120%; font-style: normal; text-indent: 0; text-transform: none; line-height: normal; letter-spacing: normal; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; z-index: 201; border-radius: 15px; -webkit-border-radius: 15px; -moz-border-radius: 15px; -khtml-border-radius: 15px; box-shadow: 0px 10px 20px #808080; -webkit-box-shadow: 0px 10px 20px #808080; -moz-box-shadow: 0px 10px 20px #808080; -khtml-box-shadow: 0px 10px 20px #808080; filter: progid:DXImageTransform.Microsoft.dropshadow(OffX=2, OffY=2, Color='gray', Positive='true')}
#MathJax_About.MathJax_MousePost {outline: none}
.MathJax_Menu {position: absolute; background-color: white; color: black; width: auto; padding: 2px; border: 1px solid #CCCCCC; margin: 0; cursor: default; font: menu; text-align: left; text-indent: 0; text-transform: none; line-height: normal; letter-spacing: normal; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; z-index: 201; box-shadow: 0px 10px 20px #808080; -webkit-box-shadow: 0px 10px 20px #808080; -moz-box-shadow: 0px 10px 20px #808080; -khtml-box-shadow: 0px 10px 20px #808080; filter: progid:DXImageTransform.Microsoft.dropshadow(OffX=2, OffY=2, Color='gray', Positive='true')}
.MathJax_MenuItem {padding: 2px 2em; background: transparent}
.MathJax_MenuArrow {position: absolute; right: .5em; padding-top: .25em; color: #666666; font-size: .75em}
.MathJax_MenuActive .MathJax_MenuArrow {color: white}
.MathJax_MenuArrow.RTL {left: .5em; right: auto}
.MathJax_MenuCheck {position: absolute; left: .7em}
.MathJax_MenuCheck.RTL {right: .7em; left: auto}
.MathJax_MenuRadioCheck {position: absolute; left: 1em}
.MathJax_MenuRadioCheck.RTL {right: 1em; left: auto}
.MathJax_MenuLabel {padding: 2px 2em 4px 1.33em; font-style: italic}
.MathJax_MenuRule {border-top: 1px solid #CCCCCC; margin: 4px 1px 0px}
.MathJax_MenuDisabled {color: GrayText}
.MathJax_MenuActive {background-color: Highlight; color: HighlightText}
.MathJax_MenuDisabled:focus, .MathJax_MenuLabel:focus {background-color: #E8E8E8}
.MathJax_ContextMenu:focus {outline: none}
.MathJax_ContextMenu .MathJax_MenuItem:focus {outline: none}
#MathJax_AboutClose {top: .2em; right: .2em}
.MathJax_Menu .MathJax_MenuClose {top: -10px; left: -10px}
.MathJax_MenuClose {position: absolute; cursor: pointer; display: inline-block; border: 2px solid #AAA; border-radius: 18px; -webkit-border-radius: 18px; -moz-border-radius: 18px; -khtml-border-radius: 18px; font-family: 'Courier New',Courier; font-size: 24px; color: #F0F0F0}
.MathJax_MenuClose span {display: block; background-color: #AAA; border: 1.5px solid; border-radius: 18px; -webkit-border-radius: 18px; -moz-border-radius: 18px; -khtml-border-radius: 18px; line-height: 0; padding: 8px 0 6px}
.MathJax_MenuClose:hover {color: white!important; border: 2px solid #CCC!important}
.MathJax_MenuClose:hover span {background-color: #CCC!important}
.MathJax_MenuClose:hover:focus {outline: none}
</style><style type="text/css">.MathJax_Preview .MJXf-math {color: inherit!important}
</style><style type="text/css">.MJX_Assistive_MathML {position: absolute!important; top: 0; left: 0; clip: rect(1px, 1px, 1px, 1px); padding: 1px 0 0 0!important; border: 0!important; height: 1px!important; width: 1px!important; overflow: hidden!important; display: block!important; -webkit-touch-callout: none; -webkit-user-select: none; -khtml-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none}
.MJX_Assistive_MathML.MJX_Assistive_MathML_Block {width: 100%!important}
</style><style type="text/css">#MathJax_Zoom {position: absolute; background-color: #F0F0F0; overflow: auto; display: block; z-index: 301; padding: .5em; border: 1px solid black; margin: 0; font-weight: normal; font-style: normal; text-align: left; text-indent: 0; text-transform: none; line-height: normal; letter-spacing: normal; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; -webkit-box-sizing: content-box; -moz-box-sizing: content-box; box-sizing: content-box; box-shadow: 5px 5px 15px #AAAAAA; -webkit-box-shadow: 5px 5px 15px #AAAAAA; -moz-box-shadow: 5px 5px 15px #AAAAAA; -khtml-box-shadow: 5px 5px 15px #AAAAAA; filter: progid:DXImageTransform.Microsoft.dropshadow(OffX=2, OffY=2, Color='gray', Positive='true')}
#MathJax_ZoomOverlay {position: absolute; left: 0; top: 0; z-index: 300; display: inline-block; width: 100%; height: 100%; border: 0; padding: 0; margin: 0; background-color: white; opacity: 0; filter: alpha(opacity=0)}
#MathJax_ZoomFrame {position: relative; display: inline-block; height: 0; width: 0}
#MathJax_ZoomEventTrap {position: absolute; left: 0; top: 0; z-index: 302; display: inline-block; border: 0; padding: 0; margin: 0; background-color: white; opacity: 0; filter: alpha(opacity=0)}
</style><style type="text/css">.MathJax_Preview {color: #888}
#MathJax_Message {position: fixed; left: 1em; bottom: 1.5em; background-color: #E6E6E6; border: 1px solid #959595; margin: 0px; padding: 2px 8px; z-index: 102; color: black; font-size: 80%; width: auto; white-space: nowrap}
#MathJax_MSIE_Frame {position: absolute; top: 0; left: 0; width: 0px; z-index: 101; border: 0px; margin: 0px; padding: 0px}
.MathJax_Error {color: #CC0000; font-style: italic}
</style><style>
		[data-pivot]:not([data-pivot~='dotnet-5-0']) {
			display: none !important;
		}</style><style type="text/css">.MJXp-script {font-size: .8em}
.MJXp-right {-webkit-transform-origin: right; -moz-transform-origin: right; -ms-transform-origin: right; -o-transform-origin: right; transform-origin: right}
.MJXp-bold {font-weight: bold}
.MJXp-italic {font-style: italic}
.MJXp-scr {font-family: MathJax_Script,'Times New Roman',Times,STIXGeneral,serif}
.MJXp-frak {font-family: MathJax_Fraktur,'Times New Roman',Times,STIXGeneral,serif}
.MJXp-sf {font-family: MathJax_SansSerif,'Times New Roman',Times,STIXGeneral,serif}
.MJXp-cal {font-family: MathJax_Caligraphic,'Times New Roman',Times,STIXGeneral,serif}
.MJXp-mono {font-family: MathJax_Typewriter,'Times New Roman',Times,STIXGeneral,serif}
.MJXp-largeop {font-size: 150%}
.MJXp-largeop.MJXp-int {vertical-align: -.2em}
.MJXp-math {display: inline-block; line-height: 1.2; text-indent: 0; font-family: 'Times New Roman',Times,STIXGeneral,serif; white-space: nowrap; border-collapse: collapse}
.MJXp-display {display: block; text-align: center; margin: 1em 0}
.MJXp-math span {display: inline-block}
.MJXp-box {display: block!important; text-align: center}
.MJXp-box:after {content: " "}
.MJXp-rule {display: block!important; margin-top: .1em}
.MJXp-char {display: block!important}
.MJXp-mo {margin: 0 .15em}
.MJXp-mfrac {margin: 0 .125em; vertical-align: .25em}
.MJXp-denom {display: inline-table!important; width: 100%}
.MJXp-denom > * {display: table-row!important}
.MJXp-surd {vertical-align: top}
.MJXp-surd > * {display: block!important}
.MJXp-script-box > *  {display: table!important; height: 50%}
.MJXp-script-box > * > * {display: table-cell!important; vertical-align: top}
.MJXp-script-box > *:last-child > * {vertical-align: bottom}
.MJXp-script-box > * > * > * {display: block!important}
.MJXp-mphantom {visibility: hidden}
.MJXp-munderover {display: inline-table!important}
.MJXp-over {display: inline-block!important; text-align: center}
.MJXp-over > * {display: block!important}
.MJXp-munderover > * {display: table-row!important}
.MJXp-mtable {vertical-align: .25em; margin: 0 .125em}
.MJXp-mtable > * {display: inline-table!important; vertical-align: middle}
.MJXp-mtr {display: table-row!important}
.MJXp-mtd {display: table-cell!important; text-align: center; padding: .5em 0 0 .5em}
.MJXp-mtr > .MJXp-mtd:first-child {padding-left: 0}
.MJXp-mtr:first-child > .MJXp-mtd {padding-top: 0}
.MJXp-mlabeledtr {display: table-row!important}
.MJXp-mlabeledtr > .MJXp-mtd:first-child {padding-left: 0}
.MJXp-mlabeledtr:first-child > .MJXp-mtd {padding-top: 0}
.MJXp-merror {background-color: #FFFF88; color: #CC0000; border: 1px solid #CC0000; padding: 1px 3px; font-style: normal; font-size: 90%}
.MJXp-scale0 {-webkit-transform: scaleX(.0); -moz-transform: scaleX(.0); -ms-transform: scaleX(.0); -o-transform: scaleX(.0); transform: scaleX(.0)}
.MJXp-scale1 {-webkit-transform: scaleX(.1); -moz-transform: scaleX(.1); -ms-transform: scaleX(.1); -o-transform: scaleX(.1); transform: scaleX(.1)}
.MJXp-scale2 {-webkit-transform: scaleX(.2); -moz-transform: scaleX(.2); -ms-transform: scaleX(.2); -o-transform: scaleX(.2); transform: scaleX(.2)}
.MJXp-scale3 {-webkit-transform: scaleX(.3); -moz-transform: scaleX(.3); -ms-transform: scaleX(.3); -o-transform: scaleX(.3); transform: scaleX(.3)}
.MJXp-scale4 {-webkit-transform: scaleX(.4); -moz-transform: scaleX(.4); -ms-transform: scaleX(.4); -o-transform: scaleX(.4); transform: scaleX(.4)}
.MJXp-scale5 {-webkit-transform: scaleX(.5); -moz-transform: scaleX(.5); -ms-transform: scaleX(.5); -o-transform: scaleX(.5); transform: scaleX(.5)}
.MJXp-scale6 {-webkit-transform: scaleX(.6); -moz-transform: scaleX(.6); -ms-transform: scaleX(.6); -o-transform: scaleX(.6); transform: scaleX(.6)}
.MJXp-scale7 {-webkit-transform: scaleX(.7); -moz-transform: scaleX(.7); -ms-transform: scaleX(.7); -o-transform: scaleX(.7); transform: scaleX(.7)}
.MJXp-scale8 {-webkit-transform: scaleX(.8); -moz-transform: scaleX(.8); -ms-transform: scaleX(.8); -o-transform: scaleX(.8); transform: scaleX(.8)}
.MJXp-scale9 {-webkit-transform: scaleX(.9); -moz-transform: scaleX(.9); -ms-transform: scaleX(.9); -o-transform: scaleX(.9); transform: scaleX(.9)}
.MathJax_PHTML .noError {vertical-align: ; font-size: 90%; text-align: left; color: black; padding: 1px 3px; border: 1px solid}
</style><script async="" src="./如何使用 C# 对 JSON 进行序列化和反序列化 - .NET _ Microsoft Docs_files/t.js(1).下载"></script><style type="text/css" media="print">.usabilla_live_button_container { display: none; }</style><script async="" src="./如何使用 C# 对 JSON 进行序列化和反序列化 - .NET _ Microsoft Docs_files/t.js(2).下载"></script><script async="" src="./如何使用 C# 对 JSON 进行序列化和反序列化 - .NET _ Microsoft Docs_files/t.js(3).下载"></script><script async="" src="./如何使用 C# 对 JSON 进行序列化和反序列化 - .NET _ Microsoft Docs_files/t.js(4).下载"></script><script async="" src="./如何使用 C# 对 JSON 进行序列化和反序列化 - .NET _ Microsoft Docs_files/t.js(5).下载"></script></head>

<body lang="zh-cn" dir="ltr"><div id="MathJax_Message" style="display: none;"></div><div style="display: none;" id="lightningjs-usabilla_live"><div><iframe frameborder="0" id="lightningjs-frame-usabilla_live" src="./如何使用 C# 对 JSON 进行序列化和反序列化 - .NET _ Microsoft Docs_files/saved_resource.html"></iframe></div></div>
	<div class="header-holder has-default-focus">
		<a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-5-0#main" class="skip-to-main-link has-outline-color-text visually-hidden-until-focused is-fixed has-inner-focus focus-visible has-top-zero has-left-zero has-right-zero has-padding-medium has-text-centered has-body-background-medium" tabindex="1">跳转至主内容</a>
		
		<div id="ignite-banner-holder" class="has-default-focus has-overflow-hidden"></div>
		
		<div hidden="" id="cookie-consent-holder"></div>
		<!-- liquid-tag banners global -->
		<div id="headerAreaHolder" data-bi-name="header">
<header role="banner" itemscope="itemscope" itemtype="http://schema.org/Organization"><!----> <div class="nav-bar"><!---->  <div class="nav-bar-brand">
		<a class="nav-bar-button" itemprop="url" href="https://www.microsoft.com/" aria-label="Microsoft">
			<div class="nav-bar-logo" role="presentation" aria-hidden="true" itemprop="logo" itemscope="itemscope">
				<svg xmlns="http://www.w3.org/2000/svg" width="108" height="23" viewBox="72 72 337 74" preserveAspectRatio="xMidYMin slice">
					<g data-name="MS-symbol">
						<clippath id="a">
							<path transform="matrix(1 0 0 -1 0 216)" d="M0 216h482V0H0z"></path>
						</clippath>
						<g clip-path="url(#a)">
							<path class="has-fill-current-color has-text-subtle" d="M394.942 104.59h-10.858v25.003h-7.38V104.59h-5.182v-5.965h5.182v-4.308c0-3.254 1.06-5.92 3.178-7.998 2.12-2.079 4.835-3.118 8.15-3.118.882 0 1.666.045 2.35.135a9.37 9.37 0 011.806.407v6.296c-.24-.14-.663-.31-1.265-.512-.603-.2-1.296-.3-2.078-.3-1.528 0-2.702.476-3.526 1.43-.824.954-1.235 2.365-1.235 4.232v3.736h10.858v-6.959l7.321-2.229v9.188h7.381v5.965h-7.38v14.49c0 1.91.345 3.254 1.038 4.037.693.783 1.782 1.175 3.27 1.175.42 0 .927-.1 1.52-.3a7.178 7.178 0 001.552-.724v6.025c-.463.261-1.23.502-2.305.723a15.742 15.742 0 01-3.178.331c-3.073 0-5.378-.817-6.914-2.455-1.537-1.637-2.305-4.102-2.305-7.396zm-48.407 9.73c0 3.233.733 5.703 2.2 7.411 1.465 1.707 3.564 2.56 6.295 2.56 2.652 0 4.67-.853 6.055-2.56 1.386-1.708 2.08-4.238 2.08-7.592 0-3.334-.719-5.849-2.155-7.547-1.436-1.697-3.45-2.545-6.04-2.545-2.67 0-4.745.888-6.22 2.666-1.477 1.777-2.215 4.313-2.215 7.607m-7.592.24c0-5.12 1.446-9.177 4.338-12.17 2.892-2.993 6.91-4.489 12.05-4.489 4.841 0 8.621 1.441 11.343 4.323 2.721 2.883 4.082 6.774 4.082 11.674 0 5.021-1.447 9.018-4.338 11.99-2.892 2.973-6.829 4.458-11.81 4.458-4.8 0-8.61-1.41-11.432-4.232-2.822-2.82-4.233-6.673-4.233-11.554m-16.417-7.802c0 1.045.331 1.863.994 2.456.662.592 2.128 1.34 4.398 2.243 2.912 1.166 4.956 2.476 6.131 3.932 1.175 1.456 1.762 3.22 1.762 5.287 0 2.912-1.12 5.252-3.359 7.02-2.24 1.767-5.267 2.65-9.083 2.65-1.285 0-2.706-.155-4.263-.467-1.556-.31-2.877-.707-3.96-1.19v-7.169a17.945 17.945 0 004.277 2.198c1.526.543 2.911.814 4.157.814 1.647 0 2.862-.23 3.645-.693.784-.46 1.175-1.235 1.175-2.319 0-1.005-.406-1.853-1.22-2.546-.813-.693-2.355-1.492-4.624-2.395-2.69-1.125-4.599-2.39-5.724-3.796-1.125-1.406-1.687-3.193-1.687-5.362 0-2.792 1.11-5.086 3.33-6.884 2.217-1.797 5.095-2.696 8.63-2.696 1.084 0 2.3.12 3.645.361 1.346.242 2.47.553 3.374.934v6.93c-.964-.644-2.089-1.195-3.374-1.658-1.286-.462-2.56-.693-3.826-.693-1.386 0-2.465.271-3.238.813-.774.543-1.16 1.286-1.16 2.23m-35.066 7.562c0 3.233.733 5.703 2.2 7.411 1.465 1.707 3.564 2.56 6.295 2.56 2.652 0 4.67-.853 6.055-2.56 1.386-1.708 2.08-4.238 2.08-7.592 0-3.334-.719-5.849-2.155-7.547-1.436-1.697-3.449-2.545-6.039-2.545-2.672 0-4.745.888-6.222 2.666-1.476 1.777-2.214 4.313-2.214 7.607m-7.592.24c0-5.12 1.446-9.177 4.338-12.17 2.893-2.993 6.91-4.489 12.051-4.489 4.84 0 8.621 1.441 11.342 4.323 2.721 2.883 4.082 6.774 4.082 11.674 0 5.021-1.446 9.018-4.338 11.99-2.892 2.973-6.828 4.458-11.809 4.458-4.8 0-8.61-1.41-11.433-4.232-2.822-2.82-4.233-6.673-4.233-11.554m-3.136-16.448c.582 0 1.105.041 1.567.121.462.08.853.18 1.175.301v7.38c-.382-.28-.939-.546-1.672-.798-.733-.25-1.622-.376-2.666-.376-1.788 0-3.299.753-4.534 2.26-1.235 1.505-1.853 3.825-1.853 6.958v15.635h-7.29V98.624h7.29v4.88h.12c.663-1.687 1.667-3.007 3.013-3.96 1.346-.954 2.962-1.432 4.85-1.432m-27.956 26.18c1.084 0 2.28-.25 3.585-.754a15.06 15.06 0 003.615-1.988v6.778c-1.165.663-2.485 1.165-3.962 1.506-1.475.342-3.098.512-4.865.512-4.559 0-8.265-1.44-11.116-4.323-2.852-2.88-4.278-6.562-4.278-11.04 0-4.98 1.456-9.083 4.37-12.307 2.91-3.224 7.037-4.835 12.38-4.835 1.366 0 2.746.176 4.143.527 1.395.352 2.504.758 3.328 1.22v6.99c-1.125-.824-2.274-1.462-3.45-1.914a9.966 9.966 0 00-3.599-.678c-2.872 0-5.192.934-6.959 2.802-1.768 1.868-2.652 4.388-2.652 7.562 0 3.132.85 5.573 2.546 7.32 1.697 1.748 4.002 2.621 6.914 2.621m-22.112 5.302h-7.29V98.624h7.29zm-8.044-39.916c0-1.205.437-2.214 1.311-3.028.874-.813 1.913-1.22 3.118-1.22 1.285 0 2.35.417 3.193 1.25.844.834 1.266 1.833 1.266 2.998 0 1.185-.432 2.18-1.296 2.982-.864.804-1.918 1.205-3.163 1.205-1.246 0-2.295-.406-3.148-1.219-.854-.815-1.28-1.803-1.28-2.968m-6.274-3.284v43.2h-7.5v-33.86h-.12l-13.408 33.86h-4.97l-13.738-33.86h-.09v33.86h-6.929v-43.2h10.755l12.412 32.024h.18l13.105-32.024z"></path>
							<path d="M106.214 106.214H71.996V71.996h34.218z" fill="#f25022"></path>
							<path d="M143.993 106.214h-34.218V71.996h34.218z" fill="#7fba00"></path>
							<path d="M106.214 143.993H71.996v-34.218h34.218z" fill="#00a4ef"></path>
							<path d="M143.993 143.993h-34.218v-34.218h34.218z" fill="#ffb900"></path>
						</g>
					</g>
				</svg>
			</div>
		</a>
	</div> 
				
		<div class="nav-bar-item is-hidden-tablet">
			<div class="nav-bar-menu">
				<button class="nav-bar-button is-title" title="全球导航" aria-expanded="false">
					<span class="nav-bar-menu-title"><!---->文档<!----></span>
					<div class="nav-bar-burger">
						<span></span>
						<span></span>
						<span></span>
						<span class="nav-bar-burger-label"><!---->全球导航<!----></span>
					</div>
				</button>
			</div>
		</div>
	
				
		<nav class="nav-bar-nav" role="navigation" aria-label="全球">
			<ul class="nav-bar-nav-list">
				
		<li class="nav-bar-item is-category">
			<a class="nav-bar-button is-title has-hover-underline" itemprop="url" href="https://docs.microsoft.com/zh-cn/">
				<span><!---->Docs<!----></span>
			</a>
		</li>
	
				<!----> <li class="nav-bar-item">
				<a class=" nav-bar-button has-hover-underline is-active" href="https://docs.microsoft.com/zh-cn/documentation">
					<span><!---->文档<!----></span>
				</a>
		  </li><!----> <li class="nav-bar-item">
				<a class=" nav-bar-button has-hover-underline " href="https://docs.microsoft.com/zh-cn/learn/">
					<span><!---->Learn<!----></span>
				</a>
		  </li><!----> <li class="nav-bar-item">
				<a class=" nav-bar-button has-hover-underline " href="https://docs.microsoft.com/zh-cn/answers/products/">
					<span><!---->Q&amp;A<!----></span>
				</a>
		  </li><!----> <li class="nav-bar-item">
				<a class=" nav-bar-button has-hover-underline " href="https://docs.microsoft.com/zh-cn/samples/browse/">
					<span><!---->代码示例<!----></span>
				</a>
		  </li><!----> <li class="dropdown nav-bar-item" hidden="">
		<button aria-expanded="false" class="dropdown-trigger nav-bar-button  has-hover-underline" aria-controls="ax-96">
			<span><!---->更多<!----></span>
			<span class="nav-bar-button-chevron" aria-hidden="true">
				<span class="docon docon-chevron-down-light expanded-indicator"></span>
			</span>
		</button>
		<ul class="dropdown-menu" id="ax-96" aria-label="更多">
			<!---->
				<li class="nav-bar-item" hidden="">
					<a class=" nav-bar-button is-active" href="https://docs.microsoft.com/zh-cn/documentation"><!---->文档<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item" hidden="">
					<a class=" nav-bar-button " href="https://docs.microsoft.com/zh-cn/learn/"><!---->Learn<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item" hidden="">
					<a class=" nav-bar-button " href="https://docs.microsoft.com/zh-cn/answers/products/"><!---->Q&amp;A<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item" hidden="">
					<a class=" nav-bar-button " href="https://docs.microsoft.com/zh-cn/samples/browse/"><!---->代码示例<!----></a>
				</li>
		  <!---->
		</ul>
	</li><!---->
			</ul>
		</nav>
	
				<span class="nav-bar-spacer"></span>
				
		<div class="nav-bar-search " id="ax-97" hidden="">
			
		<form class="nav-bar-search-form" method="GET" name="nav-bar-search-form" role="search" id="nav-bar-search-form" aria-label="搜索" aria-expanded="false" action="https://docs.microsoft.com/zh-cn/search/">
			<div class="autocomplete" data-bi-name="autocomplete"><!----> <div class="control ">
				<input role="combobox" maxlength="100" aria-autocomplete="list" autocapitalize="off" autocomplete="off" autocorrect="off" spellcheck="false" id="site-search-input" class="autocomplete-input input   is-small" type="search" name="terms" aria-expanded="false" aria-owns="ax-98-listbox" aria-activedescendant="" aria-label="搜索" placeholder="搜索">

				<span aria-hidden="true" class="icon is-small is-left" hidden="">
					<span class="has-text-primary docon docon-undefined"></span>
				</span>

				<span aria-hidden="true" class="autocomplete-loader loader has-text-primary " hidden=""></span>
			</div>

			<ul class="autocomplete-suggestions is-vertically-scrollable" role="listbox" id="ax-98-listbox" aria-label="site-search-input-suggestions" hidden="">
				
			</ul><!----></div>
			<!-- mobile safari will not dispatch submit event unless there's a submit button that is not display:none -->
			<button type="submit" class="visually-hidden" tabindex="-1" aria-hidden="true"></button>
			<input name="category" hidden="" value="">
			<input name="scope" hidden="" value=".NET">
			
			
		</form>
	
		</div>
		
					<div class="nav-bar-search">
						<!-- hidden when the width drops to tablet -->
						
		<form class="nav-bar-search-form" method="GET" name="nav-bar-search-form" role="search" id="nav-bar-search-form-desktop" aria-label="搜索" aria-expanded="false" action="https://docs.microsoft.com/zh-cn/search/">
			<div class="autocomplete" data-bi-name="autocomplete"><!----> <div class="control has-icons-left">
				<input role="combobox" maxlength="100" aria-autocomplete="list" autocapitalize="off" autocomplete="off" autocorrect="off" spellcheck="false" id="ax-99" class="autocomplete-input input control has-icons-left  is-small" type="search" name="terms" aria-expanded="false" aria-owns="ax-100-listbox" aria-activedescendant="" aria-label="搜索" placeholder="搜索">

				<span aria-hidden="true" class="icon is-small is-left">
					<span class="has-text-primary docon docon-search"></span>
				</span>

				<span aria-hidden="true" class="autocomplete-loader loader has-text-primary " hidden=""></span>
			</div>

			<ul class="autocomplete-suggestions is-vertically-scrollable" role="listbox" id="ax-100-listbox" aria-label="ax-99-suggestions" hidden="">
				
			</ul><!----></div>
			<!-- mobile safari will not dispatch submit event unless there's a submit button that is not display:none -->
			<button type="submit" class="visually-hidden" tabindex="-1" aria-hidden="true"></button>
			<input name="category" hidden="" value="">
			<input name="scope" hidden="" value=".NET">
			
			
		</form>
	
					</div>
			  
		<div class="nav-bar-search " hidden="">
			<div class="nav-bar-search-form-expander">
				<button id="form-expander" class="nav-bar-button has-hover-underline " title="搜索" aria-expanded="false" aria-controls="ax-97">
					
		<span class="is-visually-hidden-mobile"><!---->搜索<!----></span>
		<span class="icon" aria-hidden="true">
			<span class="docon docon-search"></span>
		</span>
	
				</button>
			</div>
		</div>
	
				
		<div class="nav-bar-profile">
			
		<a class="docs-sign-in auth-status-determined not-authenticated" href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-5-0#"> 登录 </a>
	 
		<div class="dropdown has-caret auth-status-determined authenticated">
			<button class="dropdown-trigger button-reset" aria-controls="user-menu" aria-expanded="false" aria-label="你的帐户">
				<figure class="image is-36x36">
					<img class="has-body-background-medium is-rounded" alt="" data-profile-property="avatarUrl">
				</figure>
			</button>
			<div class="dropdown-menu box has-flex-justify-content-start has-body-background" id="user-menu" role="menu">
				<div class="media is-user-dir">
					<div class="media-left">
						<figure class="image is-48x48" aria-hidden="true">
							<img class="has-body-background-medium is-rounded" aria-labelledby="displayName" data-profile-property="avatarUrl">
						</figure>
					</div>
					<div class="media-content">
						<p class="title is-size-large">
							<span id="displayName" class="is-inline-truncated has-max-width-200" data-profile-property="displayName"></span>
						</p>
						<p class="subtitle is-size-small has-margin-bottom-none">
							<span class="is-inline-truncated has-max-width-200" data-profile-property="upn"></span>
						</p>
					</div>
				</div>
				<ul class="has-padding-top-small has-padding-bottom-medium">
					<li class="has-padding-top-extra-small has-padding-bottom-extra-small">
						<a href="https://docs.microsoft.com/profile" data-profile-property="profileUrl"><!---->配置文件<!----></a>
					</li>
					<li class="has-padding-top-extra-small has-padding-bottom-extra-small">
						<a href="https://docs.microsoft.com/profile" data-profile-property="bookmarksUrl"><!---->书签<!----></a>
					</li>
					<li class="has-padding-top-extra-small has-padding-bottom-extra-small">
						<a href="https://docs.microsoft.com/profile" data-profile-property="collectionsUrl"><!---->集合<!----></a>
					</li>
					<li class="has-padding-top-extra-small">
						<a href="https://docs.microsoft.com/profile" data-profile-property="challengesUrl"><!---->挑战<!----></a>
					</li>
				</ul>
				<div class="has-border-top has-padding-top-medium">
					<a class="docs-sign-out" href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-5-0#"><!---->注销<!----></a>
				</div>
			</div>
		</div>
	
		</div>
	
			 <!----></div> <div class="nav-bar has-border-top is-hidden-mobile"><!----> 
		<a class="nav-bar-button is-title has-hover-underline" itemprop="url" href="https://docs.microsoft.com/dotnet/">
			<span>
				.NET
				
			</span>
		</a>
	 
		
		<nav class="nav-bar-nav" role="navigation" aria-label="站点">
			<ul class="nav-bar-nav-list">
				
				<!----> <li class="dropdown nav-bar-item">
		<button aria-expanded="false" class="dropdown-trigger nav-bar-button  has-hover-underline" aria-controls="ax-101">
			<span><!---->语言<!----></span>
			<span class="nav-bar-button-chevron" aria-hidden="true">
				<span class="docon docon-chevron-down-light expanded-indicator"></span>
			</span>
		</button>
		<ul class="dropdown-menu" id="ax-101" aria-label="语言">
			<!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://docs.microsoft.com/zh-cn/dotnet/csharp/"><!---->C#<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://docs.microsoft.com/zh-cn/dotnet/fsharp/"><!---->F#<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://docs.microsoft.com/zh-cn/dotnet/visual-basic/"><!---->Visual Basic<!----></a>
				</li>
		  <!---->
		</ul>
	</li><!----> <li class="dropdown nav-bar-item">
		<button aria-expanded="false" class="dropdown-trigger nav-bar-button  has-hover-underline" aria-controls="ax-102">
			<span><!---->工作负载<!----></span>
			<span class="nav-bar-button-chevron" aria-hidden="true">
				<span class="docon docon-chevron-down-light expanded-indicator"></span>
			</span>
		</button>
		<ul class="dropdown-menu" id="ax-102" aria-label="工作负载">
			<!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://docs.microsoft.com/zh-cn/aspnet/core/"><!---->Web<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://docs.microsoft.com/zh-cn/xamarin/"><!---->移动<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://docs.microsoft.com/zh-cn/dotnet/azure/"><!---->云<!----></a>
				</li>
		  <!----> <li class="dropdown nav-bar-item">
		<button aria-expanded="false" class="dropdown-trigger nav-bar-button  " aria-controls="ax-103">
			<span><!---->桌面<!----></span>
			<span class="nav-bar-button-chevron" aria-hidden="true">
				<span class="docon docon-chevron-right-light "></span>
			</span>
		</button>
		<ul class="dropdown-menu" id="ax-103" aria-label="桌面">
			<!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://docs.microsoft.com/zh-cn/dotnet/desktop-wpf/"><!---->Windows Presentation Foundation<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://docs.microsoft.com/zh-cn/dotnet/framework/winforms/"><!---->Windows 窗体<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://docs.microsoft.com/zh-cn/windows/uwp/"><!---->通用 Windows 应用<!----></a>
				</li>
		  <!---->
		</ul>
	</li><!----> <li class="dropdown nav-bar-item">
		<button aria-expanded="false" class="dropdown-trigger nav-bar-button  " aria-controls="ax-104">
			<span><!---->机器学习和数据<!----></span>
			<span class="nav-bar-button-chevron" aria-hidden="true">
				<span class="docon docon-chevron-right-light "></span>
			</span>
		</button>
		<ul class="dropdown-menu" id="ax-104" aria-label="机器学习和数据">
			<!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://docs.microsoft.com/zh-cn/dotnet/machine-learning/"><!---->ML.NET<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://docs.microsoft.com/zh-cn/dotnet/spark/"><!---->.NET for Apache Spark<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://docs.microsoft.com/zh-cn/ef/"><!---->实体框架<!----></a>
				</li>
		  <!---->
		</ul>
	</li><!---->
		</ul>
	</li><!----> <li class="dropdown nav-bar-item">
		<button aria-expanded="false" class="dropdown-trigger nav-bar-button  has-hover-underline" aria-controls="ax-105">
			<span><!---->API<!----></span>
			<span class="nav-bar-button-chevron" aria-hidden="true">
				<span class="docon docon-chevron-down-light expanded-indicator"></span>
			</span>
		</button>
		<ul class="dropdown-menu" id="ax-105" aria-label="API">
			<!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://docs.microsoft.com/zh-cn/dotnet/api/?view=netcore-3.1"><!---->.NET Core<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://docs.microsoft.com/zh-cn/dotnet/api/?view=netframework-4.8"><!---->.NET Framework<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://docs.microsoft.com/zh-cn/dotnet/api/?view=aspnetcore-3.1"><!---->ASP.NET<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://docs.microsoft.com/zh-cn/dotnet/api/?view=ml-dotnet"><!---->ML.NET<!----></a>
				</li>
		  <!---->
		</ul>
	</li><!----> <li class="dropdown nav-bar-item">
		<button aria-expanded="false" class="dropdown-trigger nav-bar-button  has-hover-underline" aria-controls="ax-106">
			<span><!---->资源<!----></span>
			<span class="nav-bar-button-chevron" aria-hidden="true">
				<span class="docon docon-chevron-down-light expanded-indicator"></span>
			</span>
		</button>
		<ul class="dropdown-menu" id="ax-106" aria-label="资源">
			<!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://dotnet.microsoft.com/learn/dotnet/what-is-dotnet"><!---->什么是 .NET？<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://dotnet.microsoft.com/learn/dotnet/architecture-guides"><!---->.NET 体系结构指南<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://dotnet.microsoft.com/learn"><!---->学习材料<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://dotnet.microsoft.com/download"><!---->下载<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://dotnet.microsoft.com/platform/community"><!---->社区<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://dotnet.microsoft.com/platform/support"><!---->支持<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://devblogs.microsoft.com/dotnet/"><!---->博客<!----></a>
				</li>
		  <!---->
		</ul>
	</li><!----> <li class="dropdown nav-bar-item" hidden="">
		<button aria-expanded="false" class="dropdown-trigger nav-bar-button  has-hover-underline" aria-controls="ax-107">
			<span><!---->更多<!----></span>
			<span class="nav-bar-button-chevron" aria-hidden="true">
				<span class="docon docon-chevron-down-light expanded-indicator"></span>
			</span>
		</button>
		<ul class="dropdown-menu" id="ax-107" aria-label="更多">
			<!----> <li class="dropdown nav-bar-item" hidden="">
		<button aria-expanded="false" class="dropdown-trigger nav-bar-button  " aria-controls="ax-108">
			<span><!---->语言<!----></span>
			<span class="nav-bar-button-chevron" aria-hidden="true">
				<span class="docon docon-chevron-right-light "></span>
			</span>
		</button>
		<ul class="dropdown-menu" id="ax-108" aria-label="语言">
			<!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://docs.microsoft.com/zh-cn/dotnet/csharp/"><!---->C#<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://docs.microsoft.com/zh-cn/dotnet/fsharp/"><!---->F#<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://docs.microsoft.com/zh-cn/dotnet/visual-basic/"><!---->Visual Basic<!----></a>
				</li>
		  <!---->
		</ul>
	</li><!----> <li class="dropdown nav-bar-item" hidden="">
		<button aria-expanded="false" class="dropdown-trigger nav-bar-button  " aria-controls="ax-109">
			<span><!---->工作负载<!----></span>
			<span class="nav-bar-button-chevron" aria-hidden="true">
				<span class="docon docon-chevron-right-light "></span>
			</span>
		</button>
		<ul class="dropdown-menu" id="ax-109" aria-label="工作负载">
			<!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://docs.microsoft.com/zh-cn/aspnet/core/"><!---->Web<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://docs.microsoft.com/zh-cn/xamarin/"><!---->移动<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://docs.microsoft.com/zh-cn/dotnet/azure/"><!---->云<!----></a>
				</li>
		  <!----> <li class="dropdown nav-bar-item">
		<button aria-expanded="false" class="dropdown-trigger nav-bar-button  " aria-controls="ax-110">
			<span><!---->桌面<!----></span>
			<span class="nav-bar-button-chevron" aria-hidden="true">
				<span class="docon docon-chevron-right-light "></span>
			</span>
		</button>
		<ul class="dropdown-menu" id="ax-110" aria-label="桌面">
			<!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://docs.microsoft.com/zh-cn/dotnet/desktop-wpf/"><!---->Windows Presentation Foundation<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://docs.microsoft.com/zh-cn/dotnet/framework/winforms/"><!---->Windows 窗体<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://docs.microsoft.com/zh-cn/windows/uwp/"><!---->通用 Windows 应用<!----></a>
				</li>
		  <!---->
		</ul>
	</li><!----> <li class="dropdown nav-bar-item">
		<button aria-expanded="false" class="dropdown-trigger nav-bar-button  " aria-controls="ax-111">
			<span><!---->机器学习和数据<!----></span>
			<span class="nav-bar-button-chevron" aria-hidden="true">
				<span class="docon docon-chevron-right-light "></span>
			</span>
		</button>
		<ul class="dropdown-menu" id="ax-111" aria-label="机器学习和数据">
			<!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://docs.microsoft.com/zh-cn/dotnet/machine-learning/"><!---->ML.NET<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://docs.microsoft.com/zh-cn/dotnet/spark/"><!---->.NET for Apache Spark<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://docs.microsoft.com/zh-cn/ef/"><!---->实体框架<!----></a>
				</li>
		  <!---->
		</ul>
	</li><!---->
		</ul>
	</li><!----> <li class="dropdown nav-bar-item" hidden="">
		<button aria-expanded="false" class="dropdown-trigger nav-bar-button  " aria-controls="ax-112">
			<span><!---->API<!----></span>
			<span class="nav-bar-button-chevron" aria-hidden="true">
				<span class="docon docon-chevron-right-light "></span>
			</span>
		</button>
		<ul class="dropdown-menu" id="ax-112" aria-label="API">
			<!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://docs.microsoft.com/zh-cn/dotnet/api/?view=netcore-3.1"><!---->.NET Core<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://docs.microsoft.com/zh-cn/dotnet/api/?view=netframework-4.8"><!---->.NET Framework<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://docs.microsoft.com/zh-cn/dotnet/api/?view=aspnetcore-3.1"><!---->ASP.NET<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://docs.microsoft.com/zh-cn/dotnet/api/?view=ml-dotnet"><!---->ML.NET<!----></a>
				</li>
		  <!---->
		</ul>
	</li><!----> <li class="dropdown nav-bar-item" hidden="">
		<button aria-expanded="false" class="dropdown-trigger nav-bar-button  " aria-controls="ax-113">
			<span><!---->资源<!----></span>
			<span class="nav-bar-button-chevron" aria-hidden="true">
				<span class="docon docon-chevron-right-light "></span>
			</span>
		</button>
		<ul class="dropdown-menu" id="ax-113" aria-label="资源">
			<!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://dotnet.microsoft.com/learn/dotnet/what-is-dotnet"><!---->什么是 .NET？<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://dotnet.microsoft.com/learn/dotnet/architecture-guides"><!---->.NET 体系结构指南<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://dotnet.microsoft.com/learn"><!---->学习材料<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://dotnet.microsoft.com/download"><!---->下载<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://dotnet.microsoft.com/platform/community"><!---->社区<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://dotnet.microsoft.com/platform/support"><!---->支持<!----></a>
				</li>
		  <!---->
				<li class="nav-bar-item">
					<a class="has-external-link-indicator nav-bar-button " href="https://devblogs.microsoft.com/dotnet/"><!---->博客<!----></a>
				</li>
		  <!---->
		</ul>
	</li><!---->
		</ul>
	</li><!---->
			</ul>
		</nav>
	
		<span class="nav-bar-spacer"></span>
		
		<ol class="nav-bar-list">
			 
		<li class="nav-bar-item ">
			<a class="button is-small is-primary " href="https://dotnet.microsoft.com/download">
				下载 .NET
			</a>
		</li>
	
		</ol>
	 
	 <!----></div> <!----></header>		</div>

						<div class="content-header uhf-container has-padding has-default-focus has-border-bottom-none" data-bi-name="content-header">
					<nav class="breadcrumb-holder has-padding-none has-padding-left-medium-tablet has-padding-right-medium-tablet has-padding-left-none-uhf-tablet has-padding-left-none-uhf-tablet has-padding-none-desktop has-flex-grow" data-bi-name="breadcrumb" itemscope="" itemtype="http://schema.org/BreadcrumbList" role="navigation" aria-label="痕迹">
						<ul id="page-breadcrumbs" class="breadcrumbs"><!----><!---->
			<li itemprop="itemListElement" itemscope="" itemtype="http://schema.org/ListItem">
				
							<a itemprop="item" href="https://docs.microsoft.com/zh-cn/">
								<span itemprop="name"><!---->Docs<!----></span>
							</a>
					  
				<meta itemprop="position" content="1">
			</li>
		<!---->
			<li itemprop="itemListElement" itemscope="" itemtype="http://schema.org/ListItem">
				
							<a itemprop="item" href="https://docs.microsoft.com/zh-cn/dotnet/">
								<span itemprop="name"><!---->.NET<!----></span>
							</a>
					  
				<meta itemprop="position" content="2">
			</li>
		<!---->
			<li itemprop="itemListElement" itemscope="" itemtype="http://schema.org/ListItem">
				
							<a itemprop="item" href="https://docs.microsoft.com/zh-cn/dotnet/fundamentals/">
								<span itemprop="name"><!---->.NET 基础知识<!----></span>
							</a>
					  
				<meta itemprop="position" content="3">
			</li>
		<!----><!----></ul>
					</nav>
				<div class="content-header-controls">
					<button type="button" class="contents-button button" data-bi-name="contents-expand" aria-haspopup="true">
						<span class="icon"><span class="docon docon-menu" aria-hidden="true"></span></span>
						<span class="contents-expand-title">目录</span>
					</button>
					<button type="button" class="ap-collapse-behavior ap-expanded button" data-bi-name="ap-collapse" aria-controls="action-panel">
						<span class="icon"><span class="docon docon-exit-mode" aria-hidden="true"></span></span>
						<span>退出焦点模式</span>
					</button>
				</div>
				<div class="has-padding-none-tablet has-padding-medium is-size-small is-flex-touch has-flex-justify-content-space-between-touch has-flex-grow page-action-holder">
					<ul class="is-hidden-mobile action-list has-flex-justify-content-start has-flex-justify-content-end-tablet is-flex is-flex-row has-flex-wrap has-flex-grow is-unstyled">
						<li>
							<button type="button" class="bookmark button is-text has-inner-focus is-small is-icon-only-touch" data-list-type="bookmarks" data-bi-name="bookmark" title="添加 如何使用 C# 对 JSON 进行序列化和反序列化 - .NET | Microsoft Docs 的书签" aria-pressed="false">
								<span class="icon" aria-hidden="true">
									<span class="docon docon-single-bookmark"></span>
								</span>
								<span class="bookmark-status is-visually-hidden-touch is-hidden-portrait">书签</span>
							</button>
						</li>
							<li id="feedback-section-link">
								<a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-5-0#feedback" class="button is-text has-inner-focus is-small is-icon-only-touch" data-bi-name="comments" title="发送有关此页面的反馈">
									<span class="icon" aria-hidden="true">
										<span class="docon docon-comment-lines"></span>
									</span>
									<span class="is-visually-hidden-touch is-hidden-portrait">反馈</span>
								</a>
							</li>
								<li id="contenteditbtn">
									<a href="https://github.com/dotnet/docs.zh-cn/blob/live/docs/standard/serialization/system-text-json-how-to.md" class="button is-text has-inner-focus is-icon-only-touch is-small" title="编辑本文" data-bi-name="edit" data-original_content_git_url="https://github.com/dotnet/docs.zh-cn/blob/live-sxs/docs/standard/serialization/system-text-json-how-to.md" data-original_content_git_url_template="{repo}/blob/{branch}/docs/standard/serialization/system-text-json-how-to.md" data-pr_repo="" data-pr_branch="">
									<span class="icon" aria-hidden="true">
										<span class="docon docon-edit-outline"></span>
									</span>
									<span class="is-visually-hidden-touch is-hidden-portrait">编辑</span>
								</a>
							</li>
						<li>
<div class="sharing dropdown has-caret">
	<button class="dropdown-trigger button is-text is-fullwidth has-flex-justify-content-start has-inner-focus is-small is-icon-only-touch" aria-controls="sharing-menu" aria-expanded="false" title="共享本文" data-bi-name="share">
		<span class="icon" aria-hidden="true">
			<span class="docon docon-sharing"></span>
		</span>
		<span class="is-visually-hidden-touch is-hidden-portrait">共享</span>
	</button>
	<div class="dropdown-menu has-padding-small" id="sharing-menu">
		<ul data-bi-name="share-links">
			<li>
				<a class="button is-text is-fullwidth has-flex-justify-content-start has-inner-focus is-small share-twitter" data-bi-name="twitter" href="https://twitter.com/intent/tweet?original_referer=https%3A%2F%2Fdocs.microsoft.com%2Fzh-cn%2Fdotnet%2Fstandard%2Fserialization%2Fsystem-text-json-how-to%3Fpivots%3Ddotnet-5-0%26WT.mc_id%3Dtwitter&amp;text=%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8%20C%23%20%E5%AF%B9%20JSON%20%E8%BF%9B%E8%A1%8C%E5%BA%8F%E5%88%97%E5%8C%96%E5%92%8C%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96%20-%20.NET%20%7C%20Microsoft%20Docs&amp;tw_p=tweetbutton&amp;url=https%3A%2F%2Fdocs.microsoft.com%2Fzh-cn%2Fdotnet%2Fstandard%2Fserialization%2Fsystem-text-json-how-to%3Fpivots%3Ddotnet-5-0%26WT.mc_id%3Dtwitter">
					<span class="icon">
						<span class="docon docon-brand-twitter has-text-primary" aria-hidden="true"></span>
					</span>
					<span>Twitter</span>
				</a>
			</li>
			<li>
				<a class="button is-text is-fullwidth has-flex-justify-content-start has-inner-focus is-small share-linkedin" data-bi-name="linkedin" href="https://www.linkedin.com/cws/share?url=https%3A%2F%2Fdocs.microsoft.com%2Fzh-cn%2Fdotnet%2Fstandard%2Fserialization%2Fsystem-text-json-how-to%3Fpivots%3Ddotnet-5-0%26WT.mc_id%3Dlinkedin">
					<span class="icon">
						<span class="docon docon-brand-linkedin has-text-primary" aria-hidden="true"></span>
					</span>
					<span>LinkedIn</span>
				</a>
			</li>
			<li>
				<a class="button is-text is-fullwidth has-flex-justify-content-start has-inner-focus is-small share-facebook" data-bi-name="facebook" href="https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fdocs.microsoft.com%2Fzh-cn%2Fdotnet%2Fstandard%2Fserialization%2Fsystem-text-json-how-to%3Fpivots%3Ddotnet-5-0%26WT.mc_id%3Dfacebook">
					<span class="icon">
						<span class="docon docon-brand-facebook has-text-primary" aria-hidden="true"></span>
					</span>
					<span>Facebook</span>
				</a>
			</li>
			<li>
				<a class="button is-text is-fullwidth has-flex-justify-content-start has-inner-focus is-small share-email" data-bi-name="email" href="mailto:?subject=%5B%E5%85%B1%E4%BA%AB%E6%96%87%E7%AB%A0%5D%20%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8%20C%23%20%E5%AF%B9%20JSON%20%E8%BF%9B%E8%A1%8C%E5%BA%8F%E5%88%97%E5%8C%96%E5%92%8C%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96%20-%20.NET%20%7C%20Microsoft%20Docs&amp;body=%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8%20C%23%20%E5%AF%B9%20JSON%20%E8%BF%9B%E8%A1%8C%E5%BA%8F%E5%88%97%E5%8C%96%E5%92%8C%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96%20-%20.NET%20%7C%20Microsoft%20Docs%0A%0Ahttps%3A%2F%2Fdocs.microsoft.com%2Fzh-cn%2Fdotnet%2Fstandard%2Fserialization%2Fsystem-text-json-how-to%3Fpivots%3Ddotnet-5-0%26WT.mc_id%3Demail">
					<span class="icon">
						<span class="docon docon-mail-message-fill has-text-primary" aria-hidden="true"></span>
					</span>
					<span>电子邮件</span>
				</a>
			</li>
			<li>
				<span class="button is-text is-fullwidth has-flex-justify-content-start has-inner-focus is-small share-wechat has-padding-top-extra-small has-padding-left-medium has-padding-right-medium">
					<span class="icon">
						<span class="docon docon-wechat has-text-primary" data-bi-name="wechat" aria-hidden="true"></span>
					</span>
					<span>微信</span>
				</span>
			</li>
			<li>
				<canvas class="qr-code" height="96" width="96"></canvas>
			</li>
		</ul>
	</div>
</div>						</li>
						<li>

<div class="lang-toggle-container" style="display: flex;">
	<label for="language-toggle" aria-hidden="true" tabindex="-1">使用英语阅读</label>
	<div class="checkbox-toggle-wrapper">
		<input type="checkbox" id="language-toggle" aria-label="使用英语阅读" data-m="{&quot;cN&quot;:&quot;language-toggle&quot;,&quot;value&quot;:&quot;off&quot;}">
		<label for="language-toggle" class="switch"></label>
	</div>
</div>						</li>
					</ul>
					<button type="button" class="has-border contents-button button is-small is-text is-hidden-tablet has-inner-focus" aria-label="目录" data-bi-name="contents-expand">
						<span class="icon">
							<span class="docon docon-editor-list-bullet" aria-hidden="true"></span>
						</span>
						<span class="contents-expand-title">目录</span>
					</button>
					<div class="is-invisible"></div>
					<div class="is-hidden-tablet level-item is-flexible level-right">
						<button type="button" class="page-actions-button button is-small is-text is-hidden-tablet has-inner-focus has-border is-full-height  has-margin-left-small" aria-label="页面操作" data-bi-name="pageactions">
							<span class="icon">
								<span class="docon docon-more-vertical" aria-hidden="true"></span>
							</span>
						</button>
					</div>
				</div>
			</div>



		<div id="disclaimer-holder" class="has-overflow-hidden has-default-focus">
			<!-- liquid-tag banners sectional -->
		</div>
	</div>

	<div class="mainContainer  uhf-container has-top-padding  has-default-focus" data-bi-name="body">

		<div class="columns has-large-gaps is-gapless-mobile ">

			<div id="left-container" class="left-container is-hidden-mobile column is-one-third-tablet is-one-quarter-desktop">
				<nav id="affixed-left-container" class="is-fixed is-flex is-flex-column" role="navigation" aria-label="主要" style="width: 320.25px; top: 66px; bottom: 24px;" data-bi-name="left toc"><form action="javascript:" role="search" aria-label="搜索" class="has-margin-bottom-small"><label class="visually-hidden" for="ax-114">搜索</label><div class="autocomplete is-block" data-bi-name="autocomplete"><!----> <div class="control has-icons-left">
				<input role="combobox" maxlength="100" aria-autocomplete="list" autocapitalize="off" autocomplete="off" autocorrect="off" spellcheck="false" id="ax-114" class="autocomplete-input input control has-icons-left is-full-width is-small" type="text" aria-expanded="false" aria-owns="ax-115-listbox" aria-activedescendant="" placeholder="按标题筛选">

				<span aria-hidden="true" class="icon is-small is-left">
					<span class="has-text-primary docon docon-filter-settings"></span>
				</span>

				<span aria-hidden="true" class="autocomplete-loader loader has-text-primary " hidden=""></span>
			</div>

			<ul class="autocomplete-suggestions is-vertically-scrollable" role="listbox" id="ax-115-listbox" aria-label="ax-114-suggestions" hidden="">
				
			</ul><!----></div></form><ul class="tree is-vertically-scrollable has-flex-grow has-flex-shrink" role="tree" aria-label="目录" data-bi-name="tree" data-is-collapsible="true"><li role="none"><a aria-setsize="15" aria-level="1" aria-posinset="1" role="treeitem" tabindex="-1" class="tree-item is-leaf has-external-link-indicator" data-bi-name="tree-leaf" href="https://docs.microsoft.com/zh-cn/dotnet/fundamentals/">.NET 文档</a></li><li class="tree-item" aria-setsize="15" aria-level="1" aria-posinset="2" role="treeitem" tabindex="-1" id="title-2-1" aria-expanded="false"><span data-bi-name="tree-expander" class="tree-expander"><span class="tree-expander-indicator docon docon-chevron-right-light" aria-hidden="true"></span>入门</span></li><li class="tree-item" aria-setsize="15" aria-level="1" aria-posinset="3" role="treeitem" tabindex="-1" id="title-3-1" aria-expanded="false"><span data-bi-name="tree-expander" class="tree-expander"><span class="tree-expander-indicator docon docon-chevron-right-light" aria-hidden="true"></span>概述</span></li><li class="tree-item" aria-setsize="15" aria-level="1" aria-posinset="4" role="treeitem" tabindex="-1" id="title-4-1" aria-expanded="false"><span data-bi-name="tree-expander" class="tree-expander"><span class="tree-expander-indicator docon docon-chevron-right-light" aria-hidden="true"></span>.NET 中的新增功能</span></li><li class="tree-item" aria-setsize="15" aria-level="1" aria-posinset="5" role="treeitem" tabindex="-1" id="title-5-1" aria-expanded="false"><span data-bi-name="tree-expander" class="tree-expander"><span class="tree-expander-indicator docon docon-chevron-right-light" aria-hidden="true"></span>工具和诊断</span></li><li class="tree-item" aria-setsize="15" aria-level="1" aria-posinset="6" role="treeitem" tabindex="-1" id="title-6-1" aria-expanded="false"><span data-bi-name="tree-expander" class="tree-expander"><span class="tree-expander-indicator docon docon-chevron-right-light" aria-hidden="true"></span>执行模型</span></li><li class="tree-item" aria-setsize="15" aria-level="1" aria-posinset="7" role="treeitem" tabindex="-1" id="title-7-1" aria-expanded="false"><span data-bi-name="tree-expander" class="tree-expander"><span class="tree-expander-indicator docon docon-chevron-right-light" aria-hidden="true"></span>部署模型</span></li><li class="tree-item" aria-setsize="15" aria-level="1" aria-posinset="8" role="treeitem" tabindex="-1" id="title-8-1" aria-expanded="false"><span data-bi-name="tree-expander" class="tree-expander"><span class="tree-expander-indicator docon docon-chevron-right-light" aria-hidden="true"></span>基本编码组件</span></li><li class="tree-item is-expanded" aria-setsize="15" aria-level="1" aria-posinset="9" role="treeitem" tabindex="-1" id="title-9-1" aria-expanded="true"><span data-bi-name="tree-expander" class="tree-expander"><span class="tree-expander-indicator docon docon-chevron-right-light" aria-hidden="true"></span>运行时库</span><ul class="tree-group" role="group"><li role="none"><a aria-setsize="11" aria-level="2" aria-posinset="1" role="treeitem" tabindex="-1" class="tree-item is-leaf has-external-link-indicator" data-bi-name="tree-leaf" href="https://docs.microsoft.com/zh-cn/dotnet/standard/runtime-libraries-overview">概述</a></li><li class="tree-item" aria-setsize="11" aria-level="2" aria-posinset="2" role="treeitem" tabindex="-1" id="title-9-1_2-2" aria-expanded="false"><span data-bi-name="tree-expander" class="tree-expander"><span class="tree-expander-indicator docon docon-chevron-right-light" aria-hidden="true"></span>设置数字、日期和其他类型的格式</span></li><li class="tree-item" aria-setsize="11" aria-level="2" aria-posinset="3" role="treeitem" tabindex="-1" id="title-9-1_3-2" aria-expanded="false"><span data-bi-name="tree-expander" class="tree-expander"><span class="tree-expander-indicator docon docon-chevron-right-light" aria-hidden="true"></span>使用字符串</span></li><li class="tree-item" aria-setsize="11" aria-level="2" aria-posinset="4" role="treeitem" tabindex="-1" id="title-9-1_4-2" aria-expanded="false"><span data-bi-name="tree-expander" class="tree-expander"><span class="tree-expander-indicator docon docon-chevron-right-light" aria-hidden="true"></span>正则表达式</span></li><li class="tree-item is-expanded" aria-setsize="11" aria-level="2" aria-posinset="5" role="treeitem" tabindex="-1" id="title-9-1_5-2" aria-expanded="true"><span data-bi-name="tree-expander" class="tree-expander"><span class="tree-expander-indicator docon docon-chevron-right-light" aria-hidden="true"></span>序列化</span><ul class="tree-group" role="group"><li role="none"><a aria-setsize="4" aria-level="3" aria-posinset="1" role="treeitem" tabindex="-1" class="tree-item is-leaf has-external-link-indicator" data-bi-name="tree-leaf" href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/">概述</a></li><li class="tree-item is-expanded" aria-setsize="4" aria-level="3" aria-posinset="2" role="treeitem" tabindex="-1" id="title-9-1_5-2_2-3" aria-expanded="true"><span data-bi-name="tree-expander" class="tree-expander"><span class="tree-expander-indicator docon docon-chevron-right-light" aria-hidden="true"></span>JSON 序列化</span><ul class="tree-group" role="group"><li role="none"><a aria-setsize="6" aria-level="4" aria-posinset="1" role="treeitem" tabindex="-1" class="tree-item is-leaf has-external-link-indicator" data-bi-name="tree-leaf" href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-overview">概述</a></li><li role="none"><a aria-setsize="6" aria-level="4" aria-posinset="2" role="treeitem" tabindex="0" class="tree-item is-leaf has-external-link-indicator is-selected" data-bi-name="tree-leaf" href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-how-to" aria-current="page">如何对 JSON 进行序列化和反序列化</a></li><li class="tree-item" aria-setsize="6" aria-level="4" aria-posinset="3" role="treeitem" tabindex="-1" id="title-9-1_5-2_2-3_3-4" aria-expanded="false"><span data-bi-name="tree-expander" class="tree-expander"><span class="tree-expander-indicator docon docon-chevron-right-light" aria-hidden="true"></span>控制序列化行为</span></li><li role="none"><a aria-setsize="6" aria-level="4" aria-posinset="4" role="treeitem" tabindex="-1" class="tree-item is-leaf has-external-link-indicator" data-bi-name="tree-leaf" href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-migrate-from-newtonsoft-how-to">如何从 Newtonsoft.<wbr>Json 迁移</a></li><li role="none"><a aria-setsize="6" aria-level="4" aria-posinset="5" role="treeitem" tabindex="-1" class="tree-item is-leaf has-external-link-indicator" data-bi-name="tree-leaf" href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-supported-collection-types">受支持的集合类型</a></li><li class="tree-item" aria-setsize="6" aria-level="4" aria-posinset="6" role="treeitem" tabindex="-1" id="title-9-1_5-2_2-3_6-4" aria-expanded="false"><span data-bi-name="tree-expander" class="tree-expander"><span class="tree-expander-indicator docon docon-chevron-right-light" aria-hidden="true"></span>高级</span></li></ul></li><li class="tree-item" aria-setsize="4" aria-level="3" aria-posinset="3" role="treeitem" tabindex="-1" id="title-9-1_5-2_3-3" aria-expanded="false"><span data-bi-name="tree-expander" class="tree-expander"><span class="tree-expander-indicator docon docon-chevron-right-light" aria-hidden="true"></span>二进制序列化</span></li><li class="tree-item" aria-setsize="4" aria-level="3" aria-posinset="4" role="treeitem" tabindex="-1" id="title-9-1_5-2_4-3" aria-expanded="false"><span data-bi-name="tree-expander" class="tree-expander"><span class="tree-expander-indicator docon docon-chevron-right-light" aria-hidden="true"></span>XML 和 SOAP 序列化</span></li></ul></li><li class="tree-item" aria-setsize="11" aria-level="2" aria-posinset="6" role="treeitem" tabindex="-1" id="title-9-1_6-2" aria-expanded="false"><span data-bi-name="tree-expander" class="tree-expander"><span class="tree-expander-indicator docon docon-chevron-right-light" aria-hidden="true"></span>文件和流 I/O</span></li><li role="none"><a aria-setsize="11" aria-level="2" aria-posinset="7" role="treeitem" tabindex="-1" class="tree-item is-leaf has-external-link-indicator" data-bi-name="tree-leaf" href="https://docs.microsoft.com/zh-cn/dotnet/standard/building-console-apps">System.<wbr>Console 类</a></li><li class="tree-item" aria-setsize="11" aria-level="2" aria-posinset="8" role="treeitem" tabindex="-1" id="title-9-1_8-2" aria-expanded="false"><span data-bi-name="tree-expander" class="tree-expander"><span class="tree-expander-indicator docon docon-chevron-right-light" aria-hidden="true"></span>依赖关系注入</span></li><li class="tree-item" aria-setsize="11" aria-level="2" aria-posinset="9" role="treeitem" tabindex="-1" id="title-9-1_9-2" aria-expanded="false"><span data-bi-name="tree-expander" class="tree-expander"><span class="tree-expander-indicator docon docon-chevron-right-light" aria-hidden="true"></span>Configuration</span></li><li class="tree-item" aria-setsize="11" aria-level="2" aria-posinset="10" role="treeitem" tabindex="-1" id="title-9-1_10-2" aria-expanded="false"><span data-bi-name="tree-expander" class="tree-expander"><span class="tree-expander-indicator docon docon-chevron-right-light" aria-hidden="true"></span>Logging</span></li><li role="none"><a aria-setsize="11" aria-level="2" aria-posinset="11" role="treeitem" tabindex="-1" class="tree-item is-leaf has-external-link-indicator" data-bi-name="tree-leaf" href="https://docs.microsoft.com/zh-cn/dotnet/core/extensions/generic-host">HostBuilder（泛型主机）</a></li></ul></li><li class="tree-item" aria-setsize="15" aria-level="1" aria-posinset="10" role="treeitem" tabindex="-1" id="title-10-1" aria-expanded="false"><span data-bi-name="tree-expander" class="tree-expander"><span class="tree-expander-indicator docon docon-chevron-right-light" aria-hidden="true"></span>数据访问</span></li><li class="tree-item" aria-setsize="15" aria-level="1" aria-posinset="11" role="treeitem" tabindex="-1" id="title-11-1" aria-expanded="false"><span data-bi-name="tree-expander" class="tree-expander"><span class="tree-expander-indicator docon docon-chevron-right-light" aria-hidden="true"></span>并行处理、并发和异步</span></li><li class="tree-item" aria-setsize="15" aria-level="1" aria-posinset="12" role="treeitem" tabindex="-1" id="title-12-1" aria-expanded="false"><span data-bi-name="tree-expander" class="tree-expander"><span class="tree-expander-indicator docon docon-chevron-right-light" aria-hidden="true"></span>正在测试</span></li><li role="none"><a aria-setsize="15" aria-level="1" aria-posinset="13" role="treeitem" tabindex="-1" class="tree-item is-leaf has-external-link-indicator" data-bi-name="tree-leaf" href="https://docs.microsoft.com/zh-cn/dotnet/standard/security/">安全性</a></li><li class="tree-item" aria-setsize="15" aria-level="1" aria-posinset="14" role="treeitem" tabindex="-1" id="title-14-1" aria-expanded="false"><span data-bi-name="tree-expander" class="tree-expander"><span class="tree-expander-indicator docon docon-chevron-right-light" aria-hidden="true"></span>高级主题</span></li><li class="tree-item" aria-setsize="15" aria-level="1" aria-posinset="15" role="treeitem" tabindex="-1" id="title-15-1" aria-expanded="false"><span data-bi-name="tree-expander" class="tree-expander"><span class="tree-expander-indicator docon docon-chevron-right-light" aria-hidden="true"></span>迁移指南</span></li></ul><a class="button is-small is-text has-flex-justify-content-start has-inner-focus has-margin-top-small has-border-top has-border-bottom has-flex-shrink-none" href="https://docs.microsoft.com/zh-cn/dotnet/opbuildpdf/fundamentals/toc.pdf?branch=live" data-bi-name="downloadPdf" rel="nofollow"><!----> <span class="icon is-size-extra-small has-text-subtle">
				<span aria-hidden="true" class="docon docon-dictionary-download"></span>
			</span>
			<span><!---->下载 PDF<!----></span><!----></a></nav>
			</div>

			<section class="primary-holder column is-two-thirds-tablet is-three-quarters-desktop">
				<div class="columns is-gapless-mobile has-large-gaps ">


				<div id="main-column" class="column  is-full is-four-fifths-desktop ">

					<main id="main" role="main" class="content " data-bi-name="content" lang="zh-cn" dir="ltr">



						<h1 id="how-to-serialize-and-deserialize-marshal-and-unmarshal-json-in-net"><span data-ttu-id="dd9e3-104">如何在 .NET 中对 JSON 进行序列化和反序列化（封送和拆收）</span><span class="sxs-lookup"><span data-stu-id="dd9e3-104">How to serialize and deserialize (marshal and unmarshal) JSON in .NET</span></span></h1>

						<ul class="metadata page-metadata" data-bi-name="page info" lang="zh-cn" dir="ltr">
							<li>
								<time class="" data-article-date="" aria-label="文章评审日期" datetime="2020-12-02T00:00:00.000Z" data-article-date-source="ms.date">2020/12/02</time>
							</li>
								<li class="contributors-holder">
									<ul class="facepile has-margin-left-none is-small" data-bi-name="contributors">
														<li class="facepile-item">
															<a href="https://github.com/tdykstra" title="tdykstra" aria-label="tdykstraGithub 个人资料" data-src="https://github.com/tdykstra.png?size=32" data-contributor-name="tdykstra" class="facepile-item-coin">
																<img class="facepile-item-coin-image durable-image has-error" aria-hidden="true" src="https://github.com/tdykstra.png?size=32" role="presentation" onerror="this.className=&#39;facepile-item-coin-image durable-image has-error&#39;;">
																<span class="facepile-item-coin-text durable-image-fallback facepile-item-teal" aria-hidden="true">t</span>
															</a>
														</li>
														<li class="facepile-item">
															<a href="https://github.com/olprod" title="olprod" aria-label="olprodGithub 个人资料" data-src="https://github.com/olprod.png?size=32" data-contributor-name="olprod" class="facepile-item-coin">
																<img class="facepile-item-coin-image durable-image has-error" aria-hidden="true" src="./如何使用 C# 对 JSON 进行序列化和反序列化 - .NET _ Microsoft Docs_files/olprod.png" role="presentation" onerror="this.className=&#39;facepile-item-coin-image durable-image has-error&#39;;">
																<span class="facepile-item-coin-text durable-image-fallback facepile-item-red" aria-hidden="true">o</span>
															</a>
														</li>
									</ul>
								</li>
						</ul>
		<form data-bi-name="zone-pivots" class="alert" dir="ltr" lang="zh-cn">
			
			<fieldset class="field has-padding-none" aria-label="选择 .NET 版本">
				<legend class="label">
					选择 .NET 版本
				</legend>

				<div class="buttons has-addons">
					
					<label class="button is-primary is-selected">
						<span class="icon high-contrast-only "><span aria-hidden="true" class="docon docon-location"></span></span>
						<input class="visually-hidden" type="radio" name="dotnet-version" value="dotnet-5-0" checked="">
						<span>.NET 5.0</span>
					</label>

					<label class="button ">
						<span class="icon high-contrast-only is-hidden"><span aria-hidden="true" class="docon docon-location"></span></span>
						<input class="visually-hidden" type="radio" name="dotnet-version" value="dotnet-core-3-1">
						<span>.NET Core 3.1</span>
					</label>
			</div></fieldset>
		</form>

						<nav id="center-doc-outline" class="doc-outline is-hidden-desktop" data-bi-name="intopic toc" role="navigation" aria-label="文章大纲">
							<h3>本文内容</h3>
						<ol><li><a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-5-0#namespaces">命名空间</a></li><li><a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-5-0#how-to-write-net-objects-as-json-serialize">如何将 .NET 对象编写为 JSON（序列化）</a></li><li><a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-5-0#serialize-to-utf-8">序列化为 UTF-8</a></li><li><a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-5-0#serialization-behavior">序列化行为</a></li><li><a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-5-0#how-to-read-json-as-net-objects-deserialize">如何将 JSON 读取为 .NET 对象（反序列化）</a></li><li><a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-5-0#deserialize-from-utf-8">从 UTF-8 进行反序列化</a></li><li><a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-5-0#deserialization-behavior">反序列化行为</a></li><li><a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-5-0#serialize-to-formatted-json">序列化为格式化 JSON</a></li><li><a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-5-0#include-fields">包含字段</a></li><li><a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-5-0#httpclient-and-httpcontent-extension-methods">HttpClient 和 HttpContent 扩展方法</a></li><li><a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-5-0#see-also">请参阅</a></li></ol></nav>

						<!-- <content> -->
							<p><span data-ttu-id="dd9e3-105">本文演示如何使用 <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.text.json" data-linktype="absolute-path">System.Text.Json</a> 命名空间向/从 JavaScript 对象表示法 (JSON) 进行序列化和反序列化。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-105">This article shows how to use the <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.text.json" data-linktype="absolute-path">System.Text.Json</a> namespace to serialize to and deserialize from JavaScript Object Notation (JSON).</span></span> <span data-ttu-id="dd9e3-106">如果要从 <code>Newtonsoft.Json</code> 移植现有代码，请参阅<a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-migrate-from-newtonsoft-how-to" data-linktype="relative-path">如何迁移到 <code>System.Text.Json</code></a>。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-106">If you're porting existing code from <code>Newtonsoft.Json</code>, see <a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-migrate-from-newtonsoft-how-to" data-linktype="relative-path">How to migrate to <code>System.Text.Json</code></a>.</span></span></p>
<p><span data-ttu-id="dd9e3-107">方向和示例代码直接使用库，而不是通过框架（如 <a href="https://docs.microsoft.com/zh-cn/aspnet/core/" data-linktype="absolute-path">ASP.NET Core</a>）。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-107">The directions and sample code use the library directly, not through a framework such as <a href="https://docs.microsoft.com/zh-cn/aspnet/core/" data-linktype="absolute-path">ASP.NET Core</a>.</span></span></p>
<p><span data-ttu-id="dd9e3-108">大多数序列化示例代码将 <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.text.json.jsonserializeroptions.writeindented#System_Text_Json_JsonSerializerOptions_WriteIndented" data-linktype="absolute-path">JsonSerializerOptions.WriteIndented</a> 设置为 <code>true</code>，以 JSON 进行优质打印（包含缩进和空格，以提高可读性）。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-108">Most of the serialization sample code sets <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.text.json.jsonserializeroptions.writeindented#System_Text_Json_JsonSerializerOptions_WriteIndented" data-linktype="absolute-path">JsonSerializerOptions.WriteIndented</a> to <code>true</code> to "pretty-print" the JSON (with indentation and whitespace for human readability).</span></span> <span data-ttu-id="dd9e3-109">对于生产用途，通常对于此设置会接受默认值 <code>false</code>。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-109">For production use, you would typically accept the default value of <code>false</code> for this setting.</span></span></p>
<p><span data-ttu-id="dd9e3-110">代码示例引用下面的类及其变体：</span><span class="sxs-lookup"><span data-stu-id="dd9e3-110">The code examples refer to the following class and variants of it:</span></span></p>
<div class="codeHeader" id="code-try-0" data-bi-name="code-header"><span class="language">C#</span><button type="button" class="action is-relative" data-bi-name="copy" aria-label="复制代码">
				<span class="icon">
					<span class="docon docon-edit-copy" role="presentation"></span>
				</span>
				<span>复制</span>
				<div class="successful-copy-alert is-absolute has-right-zero has-top-zero has-left-zero has-bottom-zero is-flex has-flex-align-items-center has-flex-justify-content-center has-text-success-invert has-background-success is-transparent" aria-hidden="true">
					<span class="icon is-size-large">
						<span class="docon docon-check-mark" role="presentation"></span>
					</span>
				</div>
			</button></div><pre tabindex="0" class="has-inner-focus"><code class="lang-csharp" data-author-content="public class WeatherForecast
{
    public DateTimeOffset Date { get; set; }
    public int TemperatureCelsius { get; set; }
    public string Summary { get; set; }
}
"><span><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title">WeatherForecast</span>
{
    <span class="hljs-keyword">public</span> DateTimeOffset Date { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> TemperatureCelsius { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">string</span> Summary { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }
}
</span></code></pre>
<h2 id="namespaces" class="heading-anchor"><a class="anchor-link docon docon-link" href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-5-0#namespaces" aria-labelledby="namespaces"></a><span data-ttu-id="dd9e3-111">命名空间</span><span class="sxs-lookup"><span data-stu-id="dd9e3-111">Namespaces</span></span></h2>
<p><span data-ttu-id="dd9e3-112"><a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.text.json" data-linktype="absolute-path">System.Text.Json</a> 命名空间包含所有入口点和主要类型。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-112">The <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.text.json" data-linktype="absolute-path">System.Text.Json</a> namespace contains all the entry points and the main types.</span></span> <span data-ttu-id="dd9e3-113"><a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.text.json.serialization" data-linktype="absolute-path">System.Text.Json.Serialization</a> 命名空间包含用于高级方案的特性和 API，以及特定于序列化和反序列化的自定义。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-113">The <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.text.json.serialization" data-linktype="absolute-path">System.Text.Json.Serialization</a> namespace contains attributes and APIs for advanced scenarios and customization specific to serialization and deserialization.</span></span> <span data-ttu-id="dd9e3-114">本文中演示的代码示例要求将 <code>using</code> 指令用于其中一个或两个命名空间：</span><span class="sxs-lookup"><span data-stu-id="dd9e3-114">The code examples shown in this article require <code>using</code> directives for one or both of these namespaces:</span></span></p>
<div class="codeHeader" id="code-try-1" data-bi-name="code-header"><span class="language">C#</span><button type="button" class="action is-relative" data-bi-name="copy" aria-label="复制代码">
				<span class="icon">
					<span class="docon docon-edit-copy" role="presentation"></span>
				</span>
				<span>复制</span>
				<div class="successful-copy-alert is-absolute has-right-zero has-top-zero has-left-zero has-bottom-zero is-flex has-flex-align-items-center has-flex-justify-content-center has-text-success-invert has-background-success is-transparent" aria-hidden="true">
					<span class="icon is-size-large">
						<span class="docon docon-check-mark" role="presentation"></span>
					</span>
				</div>
			</button></div><pre tabindex="0" class="has-inner-focus"><code class="lang-csharp" data-author-content="using System.Text.Json;
using System.Text.Json.Serialization;
"><span><span class="hljs-keyword">using</span> System.Text.Json;
<span class="hljs-keyword">using</span> System.Text.Json.Serialization;
</span></code></pre>
<div class="alert is-primary">
<p class="alert-title"><span class="docon docon-status-info-outline" aria-hidden="true"></span> 重要</p>
<p><span data-ttu-id="dd9e3-115"><code>System.Text.Json</code> 中不支持 <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.runtime.serialization" data-linktype="absolute-path">System.Runtime.Serialization</a> 命名空间中的特性。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-115">Attributes from the <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.runtime.serialization" data-linktype="absolute-path">System.Runtime.Serialization</a> namespace aren't supported in <code>System.Text.Json</code>.</span></span></p>
</div>
<h2 id="how-to-write-net-objects-as-json-serialize" class="heading-anchor"><a class="anchor-link docon docon-link" href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-5-0#how-to-write-net-objects-as-json-serialize" aria-labelledby="how-to-write-net-objects-as-json-serialize"></a><span data-ttu-id="dd9e3-116">如何将 .NET 对象编写为 JSON（序列化）</span><span class="sxs-lookup"><span data-stu-id="dd9e3-116">How to write .NET objects as JSON (serialize)</span></span></h2>
<p><span data-ttu-id="dd9e3-117">若要将 JSON 编写为字符串或文件，请调用 <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.text.json.jsonserializer.serialize" data-linktype="absolute-path">JsonSerializer.Serialize</a> 方法。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-117">To write JSON to a string or to a file, call the <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.text.json.jsonserializer.serialize" data-linktype="absolute-path">JsonSerializer.Serialize</a> method.</span></span></p>
<p><span data-ttu-id="dd9e3-118">下面的示例将 JSON 创建为字符串：</span><span class="sxs-lookup"><span data-stu-id="dd9e3-118">The following example creates JSON as a string:</span></span></p>
<div class="codeHeader" id="code-try-2" data-bi-name="code-header"><span class="language">C#</span><button type="button" class="action is-relative" data-bi-name="copy" aria-label="复制代码">
				<span class="icon">
					<span class="docon docon-edit-copy" role="presentation"></span>
				</span>
				<span>复制</span>
				<div class="successful-copy-alert is-absolute has-right-zero has-top-zero has-left-zero has-bottom-zero is-flex has-flex-align-items-center has-flex-justify-content-center has-text-success-invert has-background-success is-transparent" aria-hidden="true">
					<span class="icon is-size-large">
						<span class="docon docon-check-mark" role="presentation"></span>
					</span>
				</div>
			</button></div><pre tabindex="0" class="has-inner-focus"><code class="lang-csharp" data-author-content="string jsonString = JsonSerializer.Serialize(weatherForecast);
"><span><span class="hljs-keyword">string</span> jsonString = JsonSerializer.Serialize(weatherForecast);
</span></code></pre>
<p><span data-ttu-id="dd9e3-119">下面的示例使用同步代码创建 JSON 文件：</span><span class="sxs-lookup"><span data-stu-id="dd9e3-119">The following example uses synchronous code to create a JSON file:</span></span></p>
<div class="codeHeader" id="code-try-3" data-bi-name="code-header"><span class="language">C#</span><button type="button" class="action is-relative" data-bi-name="copy" aria-label="复制代码">
				<span class="icon">
					<span class="docon docon-edit-copy" role="presentation"></span>
				</span>
				<span>复制</span>
				<div class="successful-copy-alert is-absolute has-right-zero has-top-zero has-left-zero has-bottom-zero is-flex has-flex-align-items-center has-flex-justify-content-center has-text-success-invert has-background-success is-transparent" aria-hidden="true">
					<span class="icon is-size-large">
						<span class="docon docon-check-mark" role="presentation"></span>
					</span>
				</div>
			</button></div><pre tabindex="0" class="has-inner-focus"><code class="lang-csharp" data-author-content="jsonString = JsonSerializer.Serialize(weatherForecast);
File.WriteAllText(fileName, jsonString);
"><span>jsonString = JsonSerializer.Serialize(weatherForecast);
File.WriteAllText(fileName, jsonString);
</span></code></pre>
<p><span data-ttu-id="dd9e3-120">下面的示例使用异步代码创建 JSON 文件：</span><span class="sxs-lookup"><span data-stu-id="dd9e3-120">The following example uses asynchronous code to create a JSON file:</span></span></p>
<div class="codeHeader" id="code-try-4" data-bi-name="code-header"><span class="language">C#</span><button type="button" class="action is-relative" data-bi-name="copy" aria-label="复制代码">
				<span class="icon">
					<span class="docon docon-edit-copy" role="presentation"></span>
				</span>
				<span>复制</span>
				<div class="successful-copy-alert is-absolute has-right-zero has-top-zero has-left-zero has-bottom-zero is-flex has-flex-align-items-center has-flex-justify-content-center has-text-success-invert has-background-success is-transparent" aria-hidden="true">
					<span class="icon is-size-large">
						<span class="docon docon-check-mark" role="presentation"></span>
					</span>
				</div>
			</button></div><pre tabindex="0" class="has-inner-focus"><code class="lang-csharp" data-author-content="using FileStream createStream = File.Create(fileName);
await JsonSerializer.SerializeAsync(createStream, weatherForecast);
"><span><span class="hljs-keyword">using</span> FileStream createStream = File.Create(fileName);
<span class="hljs-keyword">await</span> JsonSerializer.SerializeAsync(createStream, weatherForecast);
</span></code></pre>
<p><span data-ttu-id="dd9e3-121">前面的示例对要序列化的类型使用类型推理。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-121">The preceding examples use type inference for the type being serialized.</span></span> <span data-ttu-id="dd9e3-122"><code>Serialize()</code> 的重载采用泛型类型参数：</span><span class="sxs-lookup"><span data-stu-id="dd9e3-122">An overload of <code>Serialize()</code> takes a generic type parameter:</span></span></p>
<div class="codeHeader" id="code-try-5" data-bi-name="code-header"><span class="language">C#</span><button type="button" class="action is-relative" data-bi-name="copy" aria-label="复制代码">
				<span class="icon">
					<span class="docon docon-edit-copy" role="presentation"></span>
				</span>
				<span>复制</span>
				<div class="successful-copy-alert is-absolute has-right-zero has-top-zero has-left-zero has-bottom-zero is-flex has-flex-align-items-center has-flex-justify-content-center has-text-success-invert has-background-success is-transparent" aria-hidden="true">
					<span class="icon is-size-large">
						<span class="docon docon-check-mark" role="presentation"></span>
					</span>
				</div>
			</button></div><pre tabindex="0" class="has-inner-focus"><code class="lang-csharp" data-author-content="jsonString = JsonSerializer.Serialize&lt;WeatherForecastWithPOCOs&gt;(weatherForecast);
"><span>jsonString = JsonSerializer.Serialize&lt;WeatherForecastWithPOCOs&gt;(weatherForecast);
</span></code></pre>
<h3 id="serialization-example" class="heading-anchor"><a class="anchor-link docon docon-link" href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-5-0#serialization-example" aria-labelledby="serialization-example"></a><span data-ttu-id="dd9e3-123">序列化示例</span><span class="sxs-lookup"><span data-stu-id="dd9e3-123">Serialization example</span></span></h3>
<p><span data-ttu-id="dd9e3-124">下面是一个包含集合类型属性和用户定义类型的示例类：</span><span class="sxs-lookup"><span data-stu-id="dd9e3-124">Here's an example class that contains collection-type properties and a user-defined type:</span></span></p>
<div class="codeHeader" id="code-try-6" data-bi-name="code-header"><span class="language">C#</span><button type="button" class="action is-relative" data-bi-name="copy" aria-label="复制代码">
				<span class="icon">
					<span class="docon docon-edit-copy" role="presentation"></span>
				</span>
				<span>复制</span>
				<div class="successful-copy-alert is-absolute has-right-zero has-top-zero has-left-zero has-bottom-zero is-flex has-flex-align-items-center has-flex-justify-content-center has-text-success-invert has-background-success is-transparent" aria-hidden="true">
					<span class="icon is-size-large">
						<span class="docon docon-check-mark" role="presentation"></span>
					</span>
				</div>
			</button></div><pre tabindex="0" class="has-inner-focus"><code class="lang-csharp" data-author-content="public class WeatherForecastWithPOCOs
{
    public DateTimeOffset Date { get; set; }
    public int TemperatureCelsius { get; set; }
    public string Summary { get; set; }
    public string SummaryField;
    public IList&lt;DateTimeOffset&gt; DatesAvailable { get; set; }
    public Dictionary&lt;string, HighLowTemps&gt; TemperatureRanges { get; set; }
    public string[] SummaryWords { get; set; }
}

public class HighLowTemps
{
    public int High { get; set; }
    public int Low { get; set; }
}
"><span><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title">WeatherForecastWithPOCOs</span>
{
    <span class="hljs-keyword">public</span> DateTimeOffset Date { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> TemperatureCelsius { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">string</span> Summary { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">string</span> SummaryField;
    <span class="hljs-keyword">public</span> IList&lt;DateTimeOffset&gt; DatesAvailable { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }
    <span class="hljs-keyword">public</span> Dictionary&lt;<span class="hljs-keyword">string</span>, HighLowTemps&gt; TemperatureRanges { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">string</span>[] SummaryWords { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }
}

<span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title">HighLowTemps</span>
{
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> High { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> Low { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }
}
</span></code></pre>
<div class="alert is-success">
<p class="alert-title"><span class="docon docon-lightbulb" aria-hidden="true"></span> 提示</p>
<p><span data-ttu-id="dd9e3-125">“POCO”代表<a href="https://en.wikipedia.org/wiki/Plain_old_CLR_object" data-linktype="external">普通旧 CLR 对象</a>。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-125">"POCO" stands for <a href="https://en.wikipedia.org/wiki/Plain_old_CLR_object" data-linktype="external">plain old CLR object</a>.</span></span> <span data-ttu-id="dd9e3-126">POCO 是一种 .NET 类型，它不通过继承或特性等依赖于任何框架特定类型。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-126">A POCO is a .NET type that doesn't depend on any framework-specific types, for example, through inheritance or attributes.</span></span></p>
</div>
<p><span data-ttu-id="dd9e3-127">序列化以上类型的实例的 JSON 输出类似于下面的示例。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-127">The JSON output from serializing an instance of the preceding type looks like the following example.</span></span> <span data-ttu-id="dd9e3-128">默认情况下，JSON 输出会缩小（将删除空格、缩进和换行符）：</span><span class="sxs-lookup"><span data-stu-id="dd9e3-128">The JSON output is minified (whitespace, indentation, and new-line characters are removed) by default:</span></span></p>
<div class="codeHeader" id="code-try-7" data-bi-name="code-header"><span class="language">JSON</span><button type="button" class="action is-relative" data-bi-name="copy" aria-label="复制代码">
				<span class="icon">
					<span class="docon docon-edit-copy" role="presentation"></span>
				</span>
				<span>复制</span>
				<div class="successful-copy-alert is-absolute has-right-zero has-top-zero has-left-zero has-bottom-zero is-flex has-flex-align-items-center has-flex-justify-content-center has-text-success-invert has-background-success is-transparent" aria-hidden="true">
					<span class="icon is-size-large">
						<span class="docon docon-check-mark" role="presentation"></span>
					</span>
				</div>
			</button></div><pre tabindex="0" class="has-inner-focus"><code class="lang-json" data-author-content="{&quot;Date&quot;:&quot;2019-08-01T00:00:00-07:00&quot;,&quot;TemperatureCelsius&quot;:25,&quot;Summary&quot;:&quot;Hot&quot;,&quot;DatesAvailable&quot;:[&quot;2019-08-01T00:00:00-07:00&quot;,&quot;2019-08-02T00:00:00-07:00&quot;],&quot;TemperatureRanges&quot;:{&quot;Cold&quot;:{&quot;High&quot;:20,&quot;Low&quot;:-10},&quot;Hot&quot;:{&quot;High&quot;:60,&quot;Low&quot;:20}},&quot;SummaryWords&quot;:[&quot;Cool&quot;,&quot;Windy&quot;,&quot;Humid&quot;]}
"><span>{<span class="hljs-attr">"Date"</span>:<span class="hljs-string">"2019-08-01T00:00:00-07:00"</span>,<span class="hljs-attr">"TemperatureCelsius"</span>:<span class="hljs-number">25</span>,<span class="hljs-attr">"Summary"</span>:<span class="hljs-string">"Hot"</span>,<span class="hljs-attr">"DatesAvailable"</span>:[<span class="hljs-string">"2019-08-01T00:00:00-07:00"</span>,<span class="hljs-string">"2019-08-02T00:00:00-07:00"</span>],<span class="hljs-attr">"TemperatureRanges"</span>:{<span class="hljs-attr">"Cold"</span>:{<span class="hljs-attr">"High"</span>:<span class="hljs-number">20</span>,<span class="hljs-attr">"Low"</span>:<span class="hljs-number">-10</span>},<span class="hljs-attr">"Hot"</span>:{<span class="hljs-attr">"High"</span>:<span class="hljs-number">60</span>,<span class="hljs-attr">"Low"</span>:<span class="hljs-number">20</span>}},<span class="hljs-attr">"SummaryWords"</span>:[<span class="hljs-string">"Cool"</span>,<span class="hljs-string">"Windy"</span>,<span class="hljs-string">"Humid"</span>]}
</span></code></pre>
<p><span data-ttu-id="dd9e3-129">下面的示例演示相同的 JSON，但进行了格式设置（即，具有空格和缩进的优质打印版本）：</span><span class="sxs-lookup"><span data-stu-id="dd9e3-129">The following example shows the same JSON, but formatted (that is, pretty-printed with whitespace and indentation):</span></span></p>
<div class="codeHeader" id="code-try-8" data-bi-name="code-header"><span class="language">JSON</span><button type="button" class="action is-relative" data-bi-name="copy" aria-label="复制代码">
				<span class="icon">
					<span class="docon docon-edit-copy" role="presentation"></span>
				</span>
				<span>复制</span>
				<div class="successful-copy-alert is-absolute has-right-zero has-top-zero has-left-zero has-bottom-zero is-flex has-flex-align-items-center has-flex-justify-content-center has-text-success-invert has-background-success is-transparent" aria-hidden="true">
					<span class="icon is-size-large">
						<span class="docon docon-check-mark" role="presentation"></span>
					</span>
				</div>
			</button></div><pre tabindex="0" class="has-inner-focus"><code class="lang-json" data-author-content="{
  &quot;Date&quot;: &quot;2019-08-01T00:00:00-07:00&quot;,
  &quot;TemperatureCelsius&quot;: 25,
  &quot;Summary&quot;: &quot;Hot&quot;,
  &quot;DatesAvailable&quot;: [
    &quot;2019-08-01T00:00:00-07:00&quot;,
    &quot;2019-08-02T00:00:00-07:00&quot;
  ],
  &quot;TemperatureRanges&quot;: {
    &quot;Cold&quot;: {
      &quot;High&quot;: 20,
      &quot;Low&quot;: -10
    },
    &quot;Hot&quot;: {
      &quot;High&quot;: 60,
      &quot;Low&quot;: 20
    }
  },
  &quot;SummaryWords&quot;: [
    &quot;Cool&quot;,
    &quot;Windy&quot;,
    &quot;Humid&quot;
  ]
}
"><span>{
  <span class="hljs-attr">"Date"</span>: <span class="hljs-string">"2019-08-01T00:00:00-07:00"</span>,
  <span class="hljs-attr">"TemperatureCelsius"</span>: <span class="hljs-number">25</span>,
  <span class="hljs-attr">"Summary"</span>: <span class="hljs-string">"Hot"</span>,
  <span class="hljs-attr">"DatesAvailable"</span>: [
    <span class="hljs-string">"2019-08-01T00:00:00-07:00"</span>,
    <span class="hljs-string">"2019-08-02T00:00:00-07:00"</span>
  ],
  <span class="hljs-attr">"TemperatureRanges"</span>: {
    <span class="hljs-attr">"Cold"</span>: {
      <span class="hljs-attr">"High"</span>: <span class="hljs-number">20</span>,
      <span class="hljs-attr">"Low"</span>: <span class="hljs-number">-10</span>
    },
    <span class="hljs-attr">"Hot"</span>: {
      <span class="hljs-attr">"High"</span>: <span class="hljs-number">60</span>,
      <span class="hljs-attr">"Low"</span>: <span class="hljs-number">20</span>
    }
  },
  <span class="hljs-attr">"SummaryWords"</span>: [
    <span class="hljs-string">"Cool"</span>,
    <span class="hljs-string">"Windy"</span>,
    <span class="hljs-string">"Humid"</span>
  ]
}
</span></code></pre>
<h2 id="serialize-to-utf-8" class="heading-anchor"><a class="anchor-link docon docon-link" href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-5-0#serialize-to-utf-8" aria-labelledby="serialize-to-utf-8"></a><span data-ttu-id="dd9e3-130">序列化为 UTF-8</span><span class="sxs-lookup"><span data-stu-id="dd9e3-130">Serialize to UTF-8</span></span></h2>
<p><span data-ttu-id="dd9e3-131">若要序列化为 UTF-8，请调用 <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.text.json.jsonserializer.serializetoutf8bytes" data-linktype="absolute-path">JsonSerializer.SerializeToUtf8Bytes</a> 方法：</span><span class="sxs-lookup"><span data-stu-id="dd9e3-131">To serialize to UTF-8, call the <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.text.json.jsonserializer.serializetoutf8bytes" data-linktype="absolute-path">JsonSerializer.SerializeToUtf8Bytes</a> method:</span></span></p>
<div class="codeHeader" id="code-try-9" data-bi-name="code-header"><span class="language">C#</span><button type="button" class="action is-relative" data-bi-name="copy" aria-label="复制代码">
				<span class="icon">
					<span class="docon docon-edit-copy" role="presentation"></span>
				</span>
				<span>复制</span>
				<div class="successful-copy-alert is-absolute has-right-zero has-top-zero has-left-zero has-bottom-zero is-flex has-flex-align-items-center has-flex-justify-content-center has-text-success-invert has-background-success is-transparent" aria-hidden="true">
					<span class="icon is-size-large">
						<span class="docon docon-check-mark" role="presentation"></span>
					</span>
				</div>
			</button></div><pre tabindex="0" class="has-inner-focus"><code class="lang-csharp" data-author-content="byte[] jsonUtf8Bytes;
var options = new JsonSerializerOptions
{
    WriteIndented = true
};
jsonUtf8Bytes = JsonSerializer.SerializeToUtf8Bytes(weatherForecast, options);
"><span><span class="hljs-keyword">byte</span>[] jsonUtf8Bytes;
<span class="hljs-keyword">var</span> options = <span class="hljs-keyword">new</span> JsonSerializerOptions
{
    WriteIndented = <span class="hljs-literal">true</span>
};
jsonUtf8Bytes = JsonSerializer.SerializeToUtf8Bytes(weatherForecast, options);
</span></code></pre>
<p><span data-ttu-id="dd9e3-132">还有一个采用 <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.text.json.utf8jsonwriter" data-linktype="absolute-path">Utf8JsonWriter</a> 的 <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.text.json.jsonserializer.serialize" data-linktype="absolute-path">Serialize</a> 重载可用。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-132">A <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.text.json.jsonserializer.serialize" data-linktype="absolute-path">Serialize</a> overload that takes a <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.text.json.utf8jsonwriter" data-linktype="absolute-path">Utf8JsonWriter</a> is also available.</span></span></p>
<p><span data-ttu-id="dd9e3-133">序列化为 UTF-8 比使用基于字符串的方法大约快 5-10%。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-133">Serializing to UTF-8 is about 5-10% faster than using the string-based methods.</span></span> <span data-ttu-id="dd9e3-134">出现这种差别的原因是字节（作为 UTF-8）不需要转换为字符串 (UTF-16)。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-134">The difference is because the bytes (as UTF-8) don't need to be converted to strings (UTF-16).</span></span></p>
<h2 id="serialization-behavior" class="heading-anchor"><a class="anchor-link docon docon-link" href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-5-0#serialization-behavior" aria-labelledby="serialization-behavior"></a><span data-ttu-id="dd9e3-135">序列化行为</span><span class="sxs-lookup"><span data-stu-id="dd9e3-135">Serialization behavior</span></span></h2>
<div class="zone has-pivot" data-pivot="dotnet-5-0">
<ul>
<li><span data-ttu-id="dd9e3-136">默认情况下，所有公共属性都会序列化。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-136">By default, all public properties are serialized.</span></span> <span data-ttu-id="dd9e3-137">可以<a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-ignore-properties" data-linktype="relative-path">指定要忽略的属性</a>。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-137">You can <a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-ignore-properties" data-linktype="relative-path">specify properties to ignore</a>.</span></span></li>
<li><span data-ttu-id="dd9e3-138"><a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.text.encodings.web.javascriptencoder.default#System_Text_Encodings_Web_JavaScriptEncoder_Default" data-linktype="absolute-path">默认编码器</a>会转义非 ASCII 字符、ASCII 范围内的 HTML 敏感字符以及根据 <a href="https://tools.ietf.org/html/rfc8259#section-7" data-linktype="external">RFC 8259 JSON 规范</a>必须进行转义的字符。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-138">The <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.text.encodings.web.javascriptencoder.default#System_Text_Encodings_Web_JavaScriptEncoder_Default" data-linktype="absolute-path">default encoder</a> escapes non-ASCII characters, HTML-sensitive characters within the ASCII-range, and characters that must be escaped according to <a href="https://tools.ietf.org/html/rfc8259#section-7" data-linktype="external">the RFC 8259 JSON spec</a>.</span></span></li>
<li><span data-ttu-id="dd9e3-139">默认情况下，JSON 会缩小。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-139">By default, JSON is minified.</span></span> <span data-ttu-id="dd9e3-140">可以<a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-5-0#serialize-to-formatted-json" data-linktype="self-bookmark">对 JSON 进行优质打印</a>。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-140">You can <a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-5-0#serialize-to-formatted-json" data-linktype="self-bookmark">pretty-print the JSON</a>.</span></span></li>
<li><span data-ttu-id="dd9e3-141">默认情况下，JSON 名称的大小写与 .NET 名称匹配。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-141">By default, casing of JSON names matches the .NET names.</span></span> <span data-ttu-id="dd9e3-142">可以<a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-customize-properties" data-linktype="relative-path">自定义 JSON 名称大小写</a>。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-142">You can <a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-customize-properties" data-linktype="relative-path">customize JSON name casing</a>.</span></span></li>
<li><span data-ttu-id="dd9e3-143">默认情况下，检测到循环引用并引发异常。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-143">By default, circular references are detected and exceptions thrown.</span></span> <span data-ttu-id="dd9e3-144">可以<a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-preserve-references" data-linktype="relative-path">保留引用并处理循环引用</a>。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-144">You can <a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-preserve-references" data-linktype="relative-path">preserve references and handle circular references</a>.</span></span></li>
<li><span data-ttu-id="dd9e3-145">默认情况下忽略<a href="https://docs.microsoft.com/zh-cn/dotnet/csharp/programming-guide/classes-and-structs/fields" data-linktype="relative-path">字段</a>。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-145">By default, <a href="https://docs.microsoft.com/zh-cn/dotnet/csharp/programming-guide/classes-and-structs/fields" data-linktype="relative-path">fields</a> are ignored.</span></span> <span data-ttu-id="dd9e3-146">可以<a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-5-0#include-fields" data-linktype="self-bookmark">包含字段</a>。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-146">You can <a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-5-0#include-fields" data-linktype="self-bookmark">include fields</a>.</span></span></li>
</ul>
<p><span data-ttu-id="dd9e3-147">当你在 ASP.NET Core 应用中间接使用 System.Text.Json 时，某些默认行为会有所不同。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-147">When you use System.Text.Json indirectly in an ASP.NET Core app, some default behaviors are different.</span></span> <span data-ttu-id="dd9e3-148">有关详细信息，请参阅 <a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-configure-options#web-defaults-for-jsonserializeroptions" data-linktype="relative-path">JsonSerializerOptions 的 Web 默认值</a>。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-148">For more information, see <a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-configure-options#web-defaults-for-jsonserializeroptions" data-linktype="relative-path">Web defaults for JsonSerializerOptions</a>.</span></span></p>
</div>
<div class="zone has-pivot" data-pivot="dotnet-core-3-1">
<ul>
<li><span data-ttu-id="dd9e3-149">默认情况下，所有公共属性都会序列化。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-149">By default, all public properties are serialized.</span></span> <span data-ttu-id="dd9e3-150">可以<a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-ignore-properties" data-linktype="relative-path">指定要忽略的属性</a>。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-150">You can <a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-ignore-properties" data-linktype="relative-path">specify properties to ignore</a>.</span></span></li>
<li><span data-ttu-id="dd9e3-151"><a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.text.encodings.web.javascriptencoder.default#System_Text_Encodings_Web_JavaScriptEncoder_Default" data-linktype="absolute-path">默认编码器</a>会转义非 ASCII 字符、ASCII 范围内的 HTML 敏感字符以及根据 <a href="https://tools.ietf.org/html/rfc8259#section-7" data-linktype="external">RFC 8259 JSON 规范</a>必须进行转义的字符。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-151">The <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.text.encodings.web.javascriptencoder.default#System_Text_Encodings_Web_JavaScriptEncoder_Default" data-linktype="absolute-path">default encoder</a> escapes non-ASCII characters, HTML-sensitive characters within the ASCII-range, and characters that must be escaped according to <a href="https://tools.ietf.org/html/rfc8259#section-7" data-linktype="external">the RFC 8259 JSON spec</a>.</span></span></li>
<li><span data-ttu-id="dd9e3-152">默认情况下，JSON 会缩小。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-152">By default, JSON is minified.</span></span> <span data-ttu-id="dd9e3-153">可以<a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-5-0#serialize-to-formatted-json" data-linktype="self-bookmark">对 JSON 进行优质打印</a>。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-153">You can <a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-5-0#serialize-to-formatted-json" data-linktype="self-bookmark">pretty-print the JSON</a>.</span></span></li>
<li><span data-ttu-id="dd9e3-154">默认情况下，JSON 名称的大小写与 .NET 名称匹配。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-154">By default, casing of JSON names matches the .NET names.</span></span> <span data-ttu-id="dd9e3-155">可以<a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-customize-properties" data-linktype="relative-path">自定义 JSON 名称大小写</a>。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-155">You can <a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-customize-properties" data-linktype="relative-path">customize JSON name casing</a>.</span></span></li>
<li><span data-ttu-id="dd9e3-156">检测到循环引用并引发异常。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-156">Circular references are detected and exceptions thrown.</span></span></li>
<li><span data-ttu-id="dd9e3-157">将忽略<a href="https://docs.microsoft.com/zh-cn/dotnet/csharp/programming-guide/classes-and-structs/fields" data-linktype="relative-path">字段</a>。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-157"><a href="https://docs.microsoft.com/zh-cn/dotnet/csharp/programming-guide/classes-and-structs/fields" data-linktype="relative-path">Fields</a> are ignored.</span></span></li>
</ul>
</div>
<p><span data-ttu-id="dd9e3-158">支持的类型包括：</span><span class="sxs-lookup"><span data-stu-id="dd9e3-158">Supported types include:</span></span></p>
<div class="zone has-pivot" data-pivot="dotnet-5-0">
<ul>
<li><span data-ttu-id="dd9e3-159">映射到 JavaScript 基元的 .NET 基元，如数值类型、字符串和布尔。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-159">.NET primitives that map to JavaScript primitives, such as numeric types, strings, and Boolean.</span></span></li>
<li><span data-ttu-id="dd9e3-160">用户定义的<a href="https://en.wikipedia.org/wiki/Plain_old_CLR_object" data-linktype="external">普通旧 CLR 对象 (POCO)</a>。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-160">User-defined <a href="https://en.wikipedia.org/wiki/Plain_old_CLR_object" data-linktype="external">plain old CLR objects (POCOs)</a>.</span></span></li>
<li><span data-ttu-id="dd9e3-161">一维和交错数组 (<code>T[][]</code>)。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-161">One-dimensional and jagged arrays (<code>T[][]</code>).</span></span></li>
<li><span data-ttu-id="dd9e3-162">以下命名空间中的集合和字典。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-162">Collections and dictionaries from the following namespaces.</span></span>
<ul>
<li><a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.collections" data-linktype="absolute-path">System.Collections</a></li>
<li><a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.generic" data-linktype="absolute-path">System.Collections.Generic</a></li>
<li><a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.immutable" data-linktype="absolute-path">System.Collections.Immutable</a></li>
<li><a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.concurrent" data-linktype="absolute-path">System.Collections.Concurrent</a></li>
<li><a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.specialized" data-linktype="absolute-path">System.Collections.Specialized</a></li>
<li><a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.objectmodel" data-linktype="absolute-path">System.Collections.ObjectModel</a></li>
</ul>
</li>
</ul>
</div>
<div class="zone has-pivot" data-pivot="dotnet-core-3-1">
<ul>
<li><span data-ttu-id="dd9e3-163">映射到 JavaScript 基元的 .NET 基元，如数值类型、字符串和布尔。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-163">.NET primitives that map to JavaScript primitives, such as numeric types, strings, and Boolean.</span></span></li>
<li><span data-ttu-id="dd9e3-164">用户定义的<a href="https://en.wikipedia.org/wiki/Plain_old_CLR_object" data-linktype="external">普通旧 CLR 对象 (POCO)</a>。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-164">User-defined <a href="https://en.wikipedia.org/wiki/Plain_old_CLR_object" data-linktype="external">plain old CLR objects (POCOs)</a>.</span></span></li>
<li><span data-ttu-id="dd9e3-165">一维和交错数组 (<code>ArrayName[][]</code>)。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-165">One-dimensional and jagged arrays (<code>ArrayName[][]</code>).</span></span></li>
<li><span data-ttu-id="dd9e3-166"><code>Dictionary&lt;string,TValue&gt;</code>其中 <code>TValue</code> 是 <code>object</code>、<code>JsonElement</code> 或 POCO。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-166"><code>Dictionary&lt;string,TValue&gt;</code> where <code>TValue</code> is <code>object</code>, <code>JsonElement</code>, or a POCO.</span></span></li>
<li><span data-ttu-id="dd9e3-167">以下命名空间中的集合。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-167">Collections from the following namespaces.</span></span>
<ul>
<li><a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.collections" data-linktype="absolute-path">System.Collections</a></li>
<li><a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.generic" data-linktype="absolute-path">System.Collections.Generic</a></li>
<li><a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.immutable" data-linktype="absolute-path">System.Collections.Immutable</a></li>
<li><a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.concurrent" data-linktype="absolute-path">System.Collections.Concurrent</a></li>
<li><a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.specialized" data-linktype="absolute-path">System.Collections.Specialized</a></li>
<li><a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.objectmodel" data-linktype="absolute-path">System.Collections.ObjectModel</a></li>
</ul>
</li>
</ul>
</div>
<p><span data-ttu-id="dd9e3-168">可以<a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-converters-how-to" data-linktype="relative-path">实现自定义转换器</a>以处理其他类型或提供内置转换器不支持的功能。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-168">You can <a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-converters-how-to" data-linktype="relative-path">implement custom converters</a> to handle additional types or to provide functionality that isn't supported by the built-in converters.</span></span></p>
<h2 id="how-to-read-json-as-net-objects-deserialize" class="heading-anchor"><a class="anchor-link docon docon-link" href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-5-0#how-to-read-json-as-net-objects-deserialize" aria-labelledby="how-to-read-json-as-net-objects-deserialize"></a><span data-ttu-id="dd9e3-169">如何将 JSON 读取为 .NET 对象（反序列化）</span><span class="sxs-lookup"><span data-stu-id="dd9e3-169">How to read JSON as .NET objects (deserialize)</span></span></h2>
<p><span data-ttu-id="dd9e3-170">若要从字符串或文件进行反序列化，请调用 <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.text.json.jsonserializer.deserialize" data-linktype="absolute-path">JsonSerializer.Deserialize</a> 方法。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-170">To deserialize from a string or a file, call the <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.text.json.jsonserializer.deserialize" data-linktype="absolute-path">JsonSerializer.Deserialize</a> method.</span></span></p>
<p><span data-ttu-id="dd9e3-171">下面的示例从字符串读取 JSON，并创建前面为<a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-5-0#serialization-example" data-linktype="self-bookmark">序列化示例</a>显示的 <code>WeatherForecastWithPOCOs</code> 类的实例：</span><span class="sxs-lookup"><span data-stu-id="dd9e3-171">The following example reads JSON from a string and creates an instance of the <code>WeatherForecastWithPOCOs</code> class shown earlier for the <a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-5-0#serialization-example" data-linktype="self-bookmark">serialization example</a>:</span></span></p>
<div class="codeHeader" id="code-try-10" data-bi-name="code-header"><span class="language">C#</span><button type="button" class="action is-relative" data-bi-name="copy" aria-label="复制代码">
				<span class="icon">
					<span class="docon docon-edit-copy" role="presentation"></span>
				</span>
				<span>复制</span>
				<div class="successful-copy-alert is-absolute has-right-zero has-top-zero has-left-zero has-bottom-zero is-flex has-flex-align-items-center has-flex-justify-content-center has-text-success-invert has-background-success is-transparent" aria-hidden="true">
					<span class="icon is-size-large">
						<span class="docon docon-check-mark" role="presentation"></span>
					</span>
				</div>
			</button></div><pre tabindex="0" class="has-inner-focus"><code class="lang-csharp" data-author-content="weatherForecast = JsonSerializer.Deserialize&lt;WeatherForecastWithPOCOs&gt;(jsonString);
"><span>weatherForecast = JsonSerializer.Deserialize&lt;WeatherForecastWithPOCOs&gt;(jsonString);
</span></code></pre>
<p><span data-ttu-id="dd9e3-172">若要使用同步代码从文件进行反序列化，请将文件读入字符串中，如下面的示例中所示：</span><span class="sxs-lookup"><span data-stu-id="dd9e3-172">To deserialize from a file by using synchronous code, read the file into a string, as shown in the following example:</span></span></p>
<div class="codeHeader" id="code-try-11" data-bi-name="code-header"><span class="language">C#</span><button type="button" class="action is-relative" data-bi-name="copy" aria-label="复制代码">
				<span class="icon">
					<span class="docon docon-edit-copy" role="presentation"></span>
				</span>
				<span>复制</span>
				<div class="successful-copy-alert is-absolute has-right-zero has-top-zero has-left-zero has-bottom-zero is-flex has-flex-align-items-center has-flex-justify-content-center has-text-success-invert has-background-success is-transparent" aria-hidden="true">
					<span class="icon is-size-large">
						<span class="docon docon-check-mark" role="presentation"></span>
					</span>
				</div>
			</button></div><pre tabindex="0" class="has-inner-focus"><code class="lang-csharp" data-author-content="jsonString = File.ReadAllText(fileName);
weatherForecast = JsonSerializer.Deserialize&lt;WeatherForecast&gt;(jsonString);
"><span>jsonString = File.ReadAllText(fileName);
weatherForecast = JsonSerializer.Deserialize&lt;WeatherForecast&gt;(jsonString);
</span></code></pre>
<p><span data-ttu-id="dd9e3-173">若要使用异步代码从文件进行反序列化，请调用 <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.text.json.jsonserializer.deserializeasync" data-linktype="absolute-path">DeserializeAsync</a> 方法：</span><span class="sxs-lookup"><span data-stu-id="dd9e3-173">To deserialize from a file by using asynchronous code, call the <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.text.json.jsonserializer.deserializeasync" data-linktype="absolute-path">DeserializeAsync</a> method:</span></span></p>
<div class="codeHeader" id="code-try-12" data-bi-name="code-header"><span class="language">C#</span><button type="button" class="action is-relative" data-bi-name="copy" aria-label="复制代码">
				<span class="icon">
					<span class="docon docon-edit-copy" role="presentation"></span>
				</span>
				<span>复制</span>
				<div class="successful-copy-alert is-absolute has-right-zero has-top-zero has-left-zero has-bottom-zero is-flex has-flex-align-items-center has-flex-justify-content-center has-text-success-invert has-background-success is-transparent" aria-hidden="true">
					<span class="icon is-size-large">
						<span class="docon docon-check-mark" role="presentation"></span>
					</span>
				</div>
			</button></div><pre tabindex="0" class="has-inner-focus"><code class="lang-csharp" data-author-content="using FileStream openStream = File.OpenRead(fileName);
weatherForecast = await JsonSerializer.DeserializeAsync&lt;WeatherForecast&gt;(openStream);
"><span><span class="hljs-keyword">using</span> FileStream openStream = File.OpenRead(fileName);
weatherForecast = <span class="hljs-keyword">await</span> JsonSerializer.DeserializeAsync&lt;WeatherForecast&gt;(openStream);
</span></code></pre>
<h2 id="deserialize-from-utf-8" class="heading-anchor"><a class="anchor-link docon docon-link" href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-5-0#deserialize-from-utf-8" aria-labelledby="deserialize-from-utf-8"></a><span data-ttu-id="dd9e3-174">从 UTF-8 进行反序列化</span><span class="sxs-lookup"><span data-stu-id="dd9e3-174">Deserialize from UTF-8</span></span></h2>
<p><span data-ttu-id="dd9e3-175">若要从 UTF-8 进行反序列化，请调用采用 <code>ReadOnlySpan&lt;byte&gt;</code> 或 <code>Utf8JsonReader</code> 的 <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.text.json.jsonserializer.deserialize" data-linktype="absolute-path">JsonSerializer.Deserialize</a> 重载，如下面的示例中所示。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-175">To deserialize from UTF-8, call a <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.text.json.jsonserializer.deserialize" data-linktype="absolute-path">JsonSerializer.Deserialize</a> overload that takes a <code>ReadOnlySpan&lt;byte&gt;</code> or a <code>Utf8JsonReader</code>, as shown in the following examples.</span></span> <span data-ttu-id="dd9e3-176">这些示例假设 JSON 处于名为 jsonUtf8Bytes 的字节数组中。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-176">The examples assume the JSON is in a byte array named jsonUtf8Bytes.</span></span></p>
<div class="codeHeader" id="code-try-13" data-bi-name="code-header"><span class="language">C#</span><button type="button" class="action is-relative" data-bi-name="copy" aria-label="复制代码">
				<span class="icon">
					<span class="docon docon-edit-copy" role="presentation"></span>
				</span>
				<span>复制</span>
				<div class="successful-copy-alert is-absolute has-right-zero has-top-zero has-left-zero has-bottom-zero is-flex has-flex-align-items-center has-flex-justify-content-center has-text-success-invert has-background-success is-transparent" aria-hidden="true">
					<span class="icon is-size-large">
						<span class="docon docon-check-mark" role="presentation"></span>
					</span>
				</div>
			</button></div><pre tabindex="0" class="has-inner-focus"><code class="lang-csharp" data-author-content="var readOnlySpan = new ReadOnlySpan&lt;byte&gt;(jsonUtf8Bytes);
weatherForecast = JsonSerializer.Deserialize&lt;WeatherForecast&gt;(readOnlySpan);
"><span><span class="hljs-keyword">var</span> readOnlySpan = <span class="hljs-keyword">new</span> ReadOnlySpan&lt;<span class="hljs-keyword">byte</span>&gt;(jsonUtf8Bytes);
weatherForecast = JsonSerializer.Deserialize&lt;WeatherForecast&gt;(readOnlySpan);
</span></code></pre>
<div class="codeHeader" id="code-try-14" data-bi-name="code-header"><span class="language">C#</span><button type="button" class="action is-relative" data-bi-name="copy" aria-label="复制代码">
				<span class="icon">
					<span class="docon docon-edit-copy" role="presentation"></span>
				</span>
				<span>复制</span>
				<div class="successful-copy-alert is-absolute has-right-zero has-top-zero has-left-zero has-bottom-zero is-flex has-flex-align-items-center has-flex-justify-content-center has-text-success-invert has-background-success is-transparent" aria-hidden="true">
					<span class="icon is-size-large">
						<span class="docon docon-check-mark" role="presentation"></span>
					</span>
				</div>
			</button></div><pre tabindex="0" class="has-inner-focus"><code class="lang-csharp" data-author-content="var utf8Reader = new Utf8JsonReader(jsonUtf8Bytes);
weatherForecast = JsonSerializer.Deserialize&lt;WeatherForecast&gt;(ref utf8Reader);
"><span><span class="hljs-keyword">var</span> utf8Reader = <span class="hljs-keyword">new</span> Utf8JsonReader(jsonUtf8Bytes);
weatherForecast = JsonSerializer.Deserialize&lt;WeatherForecast&gt;(<span class="hljs-keyword">ref</span> utf8Reader);
</span></code></pre>
<h2 id="deserialization-behavior" class="heading-anchor"><a class="anchor-link docon docon-link" href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-5-0#deserialization-behavior" aria-labelledby="deserialization-behavior"></a><span data-ttu-id="dd9e3-177">反序列化行为</span><span class="sxs-lookup"><span data-stu-id="dd9e3-177">Deserialization behavior</span></span></h2>
<p><span data-ttu-id="dd9e3-178">对 JSON 进行反序列化时，以下行为适用：</span><span class="sxs-lookup"><span data-stu-id="dd9e3-178">The following behaviors apply when deserializing JSON:</span></span></p>
<div class="zone has-pivot" data-pivot="dotnet-5-0">
<ul>
<li><span data-ttu-id="dd9e3-179">默认情况下，属性名称匹配区分大小写。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-179">By default, property name matching is case-sensitive.</span></span> <span data-ttu-id="dd9e3-180">可以<a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-character-casing" data-linktype="relative-path">指定不区分大小写</a>。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-180">You can <a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-character-casing" data-linktype="relative-path">specify case-insensitivity</a>.</span></span></li>
<li><span data-ttu-id="dd9e3-181">如果 JSON 包含只读属性的值，则会忽略该值，并且不引发异常。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-181">If the JSON contains a value for a read-only property, the value is ignored and no exception is thrown.</span></span></li>
<li><span data-ttu-id="dd9e3-182">序列化程序会忽略非公共构造函数。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-182">Non-public constructors are ignored by the serializer.</span></span></li>
<li><span data-ttu-id="dd9e3-183">支持反序列化为不可变对象或只读属性。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-183">Deserialization to immutable objects or read-only properties is supported.</span></span> <span data-ttu-id="dd9e3-184">请参阅<a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-immutability" data-linktype="relative-path">不可变类型和记录</a>。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-184">See <a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-immutability" data-linktype="relative-path">Immutable types and Records</a>.</span></span></li>
<li><span data-ttu-id="dd9e3-185">默认情况下，支持将枚举作为数字。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-185">By default, enums are supported as numbers.</span></span> <span data-ttu-id="dd9e3-186">可以<a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-customize-properties#enums-as-strings" data-linktype="relative-path">将枚举名称序列化为字符串</a>。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-186">You can <a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-customize-properties#enums-as-strings" data-linktype="relative-path">serialize enum names as strings</a>.</span></span></li>
<li><span data-ttu-id="dd9e3-187">默认情况下忽略字段。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-187">By default, fields are ignored.</span></span> <span data-ttu-id="dd9e3-188">可以<a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-5-0#include-fields" data-linktype="self-bookmark">包含字段</a>。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-188">You can <a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-5-0#include-fields" data-linktype="self-bookmark">include fields</a>.</span></span></li>
<li><span data-ttu-id="dd9e3-189">默认情况下，JSON 中的注释或尾随逗号会引发异常。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-189">By default, comments or trailing commas in the JSON throw exceptions.</span></span> <span data-ttu-id="dd9e3-190">可以<a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-invalid-json" data-linktype="relative-path">允许注释和尾随逗号</a>。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-190">You can <a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-invalid-json" data-linktype="relative-path">allow comments and trailing commas</a>.</span></span></li>
<li><span data-ttu-id="dd9e3-191"><a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.text.json.jsonreaderoptions.maxdepth#System_Text_Json_JsonReaderOptions_MaxDepth" data-linktype="absolute-path">默认最大深度</a>为 64。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-191">The <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.text.json.jsonreaderoptions.maxdepth#System_Text_Json_JsonReaderOptions_MaxDepth" data-linktype="absolute-path">default maximum depth</a> is 64.</span></span></li>
</ul>
<p><span data-ttu-id="dd9e3-192">当你在 ASP.NET Core 应用中间接使用 System.Text.Json 时，某些默认行为会有所不同。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-192">When you use System.Text.Json indirectly in an ASP.NET Core app, some default behaviors are different.</span></span> <span data-ttu-id="dd9e3-193">有关详细信息，请参阅 <a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-configure-options#web-defaults-for-jsonserializeroptions" data-linktype="relative-path">JsonSerializerOptions 的 Web 默认值</a>。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-193">For more information, see <a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-configure-options#web-defaults-for-jsonserializeroptions" data-linktype="relative-path">Web defaults for JsonSerializerOptions</a>.</span></span></p>
</div>
<div class="zone has-pivot" data-pivot="dotnet-core-3-1">
<ul>
<li><span data-ttu-id="dd9e3-194">默认情况下，属性名称匹配区分大小写。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-194">By default, property name matching is case-sensitive.</span></span> <span data-ttu-id="dd9e3-195">可以<a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-character-casing" data-linktype="relative-path">指定不区分大小写</a>。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-195">You can <a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-character-casing" data-linktype="relative-path">specify case-insensitivity</a>.</span></span> <span data-ttu-id="dd9e3-196">ASP.NET Core 应用<a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-configure-options#web-defaults-for-jsonserializeroptions" data-linktype="relative-path">默认指定不区分大小写</a>。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-196">ASP.NET Core apps <a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-configure-options#web-defaults-for-jsonserializeroptions" data-linktype="relative-path">specify case-insensitivity by default</a>.</span></span></li>
<li><span data-ttu-id="dd9e3-197">如果 JSON 包含只读属性的值，则会忽略该值，并且不引发异常。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-197">If the JSON contains a value for a read-only property, the value is ignored and no exception is thrown.</span></span></li>
<li><span data-ttu-id="dd9e3-198">无参数构造函数（可以是公共的、内部的或专用的）用于反序列化。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-198">A parameterless constructor, which can be public, internal, or private, is used for deserialization.</span></span></li>
<li><span data-ttu-id="dd9e3-199">不支持反序列化为不可变对象或只读属性。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-199">Deserialization to immutable objects or read-only properties isn't supported.</span></span></li>
<li><span data-ttu-id="dd9e3-200">默认情况下，支持将枚举作为数字。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-200">By default, enums are supported as numbers.</span></span> <span data-ttu-id="dd9e3-201">可以<a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-customize-properties#enums-as-strings" data-linktype="relative-path">将枚举名称序列化为字符串</a>。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-201">You can <a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-customize-properties#enums-as-strings" data-linktype="relative-path">serialize enum names as strings</a>.</span></span></li>
<li><span data-ttu-id="dd9e3-202">不支持字段。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-202">Fields aren't supported.</span></span></li>
<li><span data-ttu-id="dd9e3-203">默认情况下，JSON 中的注释或尾随逗号会引发异常。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-203">By default, comments or trailing commas in the JSON throw exceptions.</span></span> <span data-ttu-id="dd9e3-204">可以<a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-invalid-json" data-linktype="relative-path">允许注释和尾随逗号</a>。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-204">You can <a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-invalid-json" data-linktype="relative-path">allow comments and trailing commas</a>.</span></span></li>
<li><span data-ttu-id="dd9e3-205"><a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.text.json.jsonreaderoptions.maxdepth#System_Text_Json_JsonReaderOptions_MaxDepth" data-linktype="absolute-path">默认最大深度</a>为 64。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-205">The <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.text.json.jsonreaderoptions.maxdepth#System_Text_Json_JsonReaderOptions_MaxDepth" data-linktype="absolute-path">default maximum depth</a> is 64.</span></span></li>
</ul>
<p><span data-ttu-id="dd9e3-206">当你在 ASP.NET Core 应用中间接使用 System.Text.Json 时，某些默认行为会有所不同。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-206">When you use System.Text.Json indirectly in an ASP.NET Core app, some default behaviors are different.</span></span> <span data-ttu-id="dd9e3-207">有关详细信息，请参阅 <a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-configure-options#web-defaults-for-jsonserializeroptions" data-linktype="relative-path">JsonSerializerOptions 的 Web 默认值</a>。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-207">For more information, see <a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-configure-options#web-defaults-for-jsonserializeroptions" data-linktype="relative-path">Web defaults for JsonSerializerOptions</a>.</span></span></p>
</div>
<p><span data-ttu-id="dd9e3-208">可以<a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-converters-how-to" data-linktype="relative-path">实现自定义转换器</a>以提供内置转换器不支持的功能。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-208">You can <a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-converters-how-to" data-linktype="relative-path">implement custom converters</a> to provide functionality that isn't supported by the built-in converters.</span></span></p>
<h2 id="serialize-to-formatted-json" class="heading-anchor"><a class="anchor-link docon docon-link" href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-5-0#serialize-to-formatted-json" aria-labelledby="serialize-to-formatted-json"></a><span data-ttu-id="dd9e3-209">序列化为格式化 JSON</span><span class="sxs-lookup"><span data-stu-id="dd9e3-209">Serialize to formatted JSON</span></span></h2>
<p><span data-ttu-id="dd9e3-210">若要对 JSON 输出进行优质打印，请将 <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.text.json.jsonserializeroptions.writeindented#System_Text_Json_JsonSerializerOptions_WriteIndented" data-linktype="absolute-path">JsonSerializerOptions.WriteIndented</a> 设置为 <code>true</code>：</span><span class="sxs-lookup"><span data-stu-id="dd9e3-210">To pretty-print the JSON output, set <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.text.json.jsonserializeroptions.writeindented#System_Text_Json_JsonSerializerOptions_WriteIndented" data-linktype="absolute-path">JsonSerializerOptions.WriteIndented</a> to <code>true</code>:</span></span></p>
<div class="codeHeader" id="code-try-15" data-bi-name="code-header"><span class="language">C#</span><button type="button" class="action is-relative" data-bi-name="copy" aria-label="复制代码">
				<span class="icon">
					<span class="docon docon-edit-copy" role="presentation"></span>
				</span>
				<span>复制</span>
				<div class="successful-copy-alert is-absolute has-right-zero has-top-zero has-left-zero has-bottom-zero is-flex has-flex-align-items-center has-flex-justify-content-center has-text-success-invert has-background-success is-transparent" aria-hidden="true">
					<span class="icon is-size-large">
						<span class="docon docon-check-mark" role="presentation"></span>
					</span>
				</div>
			</button></div><pre tabindex="0" class="has-inner-focus"><code class="lang-csharp" data-author-content="var options = new JsonSerializerOptions
{
    WriteIndented = true,
};
jsonString = JsonSerializer.Serialize(weatherForecast, options);
"><span><span class="hljs-keyword">var</span> options = <span class="hljs-keyword">new</span> JsonSerializerOptions
{
    WriteIndented = <span class="hljs-literal">true</span>,
};
jsonString = JsonSerializer.Serialize(weatherForecast, options);
</span></code></pre>
<p><span data-ttu-id="dd9e3-211">下面是一个要进行序列化的示例类型，以及进行了优质打印的 JSON 输出：</span><span class="sxs-lookup"><span data-stu-id="dd9e3-211">Here's an example type to be serialized and pretty-printed JSON output:</span></span></p>
<div class="codeHeader" id="code-try-16" data-bi-name="code-header"><span class="language">C#</span><button type="button" class="action is-relative" data-bi-name="copy" aria-label="复制代码">
				<span class="icon">
					<span class="docon docon-edit-copy" role="presentation"></span>
				</span>
				<span>复制</span>
				<div class="successful-copy-alert is-absolute has-right-zero has-top-zero has-left-zero has-bottom-zero is-flex has-flex-align-items-center has-flex-justify-content-center has-text-success-invert has-background-success is-transparent" aria-hidden="true">
					<span class="icon is-size-large">
						<span class="docon docon-check-mark" role="presentation"></span>
					</span>
				</div>
			</button></div><pre tabindex="0" class="has-inner-focus"><code class="lang-csharp" data-author-content="public class WeatherForecast
{
    public DateTimeOffset Date { get; set; }
    public int TemperatureCelsius { get; set; }
    public string Summary { get; set; }
}
"><span><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title">WeatherForecast</span>
{
    <span class="hljs-keyword">public</span> DateTimeOffset Date { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> TemperatureCelsius { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">string</span> Summary { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }
}
</span></code></pre>
<div class="codeHeader" id="code-try-17" data-bi-name="code-header"><span class="language">JSON</span><button type="button" class="action is-relative" data-bi-name="copy" aria-label="复制代码">
				<span class="icon">
					<span class="docon docon-edit-copy" role="presentation"></span>
				</span>
				<span>复制</span>
				<div class="successful-copy-alert is-absolute has-right-zero has-top-zero has-left-zero has-bottom-zero is-flex has-flex-align-items-center has-flex-justify-content-center has-text-success-invert has-background-success is-transparent" aria-hidden="true">
					<span class="icon is-size-large">
						<span class="docon docon-check-mark" role="presentation"></span>
					</span>
				</div>
			</button></div><pre tabindex="0" class="has-inner-focus"><code class="lang-json" data-author-content="{
  &quot;Date&quot;: &quot;2019-08-01T00:00:00-07:00&quot;,
  &quot;TemperatureCelsius&quot;: 25,
  &quot;Summary&quot;: &quot;Hot&quot;
}
"><span>{
  <span class="hljs-attr">"Date"</span>: <span class="hljs-string">"2019-08-01T00:00:00-07:00"</span>,
  <span class="hljs-attr">"TemperatureCelsius"</span>: <span class="hljs-number">25</span>,
  <span class="hljs-attr">"Summary"</span>: <span class="hljs-string">"Hot"</span>
}
</span></code></pre>
<p><span data-ttu-id="dd9e3-212">如果你通过相同的选项重复使用 <code>JsonSerializerOptions</code>，则请勿在每次使用时都创建新的 <code>JsonSerializerOptions</code> 实例。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-212">If you use <code>JsonSerializerOptions</code> repeatedly with the same options, don't create a new <code>JsonSerializerOptions</code> instance each time you use it.</span></span> <span data-ttu-id="dd9e3-213">对每个调用重复使用同一实例。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-213">Reuse the same instance for every call.</span></span> <span data-ttu-id="dd9e3-214">有关详细信息，请参阅<a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-configure-options#reuse-jsonserializeroptions-instances" data-linktype="relative-path">重用 JsonSerializerOptions 实例</a>。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-214">For more information, see <a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-configure-options#reuse-jsonserializeroptions-instances" data-linktype="relative-path">Reuse JsonSerializerOptions instances</a>.</span></span></p>
<h2 id="include-fields" class="heading-anchor"><a class="anchor-link docon docon-link" href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-5-0#include-fields" aria-labelledby="include-fields"></a><span data-ttu-id="dd9e3-215">包含字段</span><span class="sxs-lookup"><span data-stu-id="dd9e3-215">Include fields</span></span></h2>
<div class="zone has-pivot" data-pivot="dotnet-5-0">
<p><span data-ttu-id="dd9e3-216">在序列化或反序列化时，使用 <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.text.json.jsonserializeroptions.includefields#System_Text_Json_JsonSerializerOptions_IncludeFields" data-linktype="absolute-path">JsonSerializerOptions.IncludeFields</a> 全局设置或 <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.text.json.serialization.jsonincludeattribute" data-linktype="absolute-path">[JsonInclude]</a> 特性来包含字段，如以下示例中所示：</span><span class="sxs-lookup"><span data-stu-id="dd9e3-216">Use the <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.text.json.jsonserializeroptions.includefields#System_Text_Json_JsonSerializerOptions_IncludeFields" data-linktype="absolute-path">JsonSerializerOptions.IncludeFields</a> global setting or the <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.text.json.serialization.jsonincludeattribute" data-linktype="absolute-path">[JsonInclude]</a> attribute to include fields when serializing or deserializing, as shown in the following example:</span></span></p>
<div class="codeHeader" id="code-try-18" data-bi-name="code-header"><span class="language">C#</span><button type="button" class="action is-relative" data-bi-name="copy" aria-label="复制代码">
				<span class="icon">
					<span class="docon docon-edit-copy" role="presentation"></span>
				</span>
				<span>复制</span>
				<div class="successful-copy-alert is-absolute has-right-zero has-top-zero has-left-zero has-bottom-zero is-flex has-flex-align-items-center has-flex-justify-content-center has-text-success-invert has-background-success is-transparent" aria-hidden="true">
					<span class="icon is-size-large">
						<span class="docon docon-check-mark" role="presentation"></span>
					</span>
				</div>
			</button></div><pre tabindex="0" class="has-inner-focus"><code class="lang-csharp" highlight-lines="16,18,20,32-35" data-author-content="using System;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Fields
{
    public class Forecast
    {
        public DateTime Date;
        public int TemperatureC;
        public string Summary;
    }

    public class Forecast2
    {
        [JsonInclude]
        public DateTime Date;
        [JsonInclude]
        public int TemperatureC;
        [JsonInclude]
        public string Summary;
    }

    public class Program
    {
        public static void Main()
        {
            var json =
                @&quot;{&quot;&quot;Date&quot;&quot;:&quot;&quot;2020-09-06T11:31:01.923395&quot;&quot;,&quot;&quot;TemperatureC&quot;&quot;:-1,&quot;&quot;Summary&quot;&quot;:&quot;&quot;Cold&quot;&quot;} &quot;;
            Console.WriteLine($&quot;Input JSON: {json}&quot;);

            var options = new JsonSerializerOptions
            {
                IncludeFields = true,
            };
            var forecast = JsonSerializer.Deserialize&lt;Forecast&gt;(json, options);

            Console.WriteLine($&quot;forecast.Date: {forecast.Date}&quot;);
            Console.WriteLine($&quot;forecast.TemperatureC: {forecast.TemperatureC}&quot;);
            Console.WriteLine($&quot;forecast.Summary: {forecast.Summary}&quot;);

            var roundTrippedJson =
                JsonSerializer.Serialize&lt;Forecast&gt;(forecast, options);

            Console.WriteLine($&quot;Output JSON: {roundTrippedJson}&quot;);

            var forecast2 = JsonSerializer.Deserialize&lt;Forecast2&gt;(json);

            Console.WriteLine($&quot;forecast2.Date: {forecast2.Date}&quot;);
            Console.WriteLine($&quot;forecast2.TemperatureC: {forecast2.TemperatureC}&quot;);
            Console.WriteLine($&quot;forecast2.Summary: {forecast2.Summary}&quot;);

            roundTrippedJson = JsonSerializer.Serialize&lt;Forecast2&gt;(forecast2);
            
            Console.WriteLine($&quot;Output JSON: {roundTrippedJson}&quot;);
        }
    }
}

// Produces output like the following example:
//
//Input JSON: { &quot;Date&quot;:&quot;2020-09-06T11:31:01.923395&quot;,&quot;TemperatureC&quot;:-1,&quot;Summary&quot;:&quot;Cold&quot;}
//forecast.Date: 9/6/2020 11:31:01 AM
//forecast.TemperatureC: -1
//forecast.Summary: Cold
//Output JSON: { &quot;Date&quot;:&quot;2020-09-06T11:31:01.923395&quot;,&quot;TemperatureC&quot;:-1,&quot;Summary&quot;:&quot;Cold&quot;}
//forecast2.Date: 9/6/2020 11:31:01 AM
//forecast2.TemperatureC: -1
//forecast2.Summary: Cold
//Output JSON: { &quot;Date&quot;:&quot;2020-09-06T11:31:01.923395&quot;,&quot;TemperatureC&quot;:-1,&quot;Summary&quot;:&quot;Cold&quot;}
"><span><span class="hljs-keyword">using</span> System;
<span class="hljs-keyword">using</span> System.Text.Json;
<span class="hljs-keyword">using</span> System.Text.Json.Serialization;

<span class="hljs-keyword">namespace</span> <span class="hljs-title">Fields</span>
{
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title">Forecast</span>
    {
        <span class="hljs-keyword">public</span> DateTime Date;
        <span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> TemperatureC;
        <span class="hljs-keyword">public</span> <span class="hljs-keyword">string</span> Summary;
    }

    <span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title">Forecast2</span>
    {</span>
<mark>        [<span class="hljs-meta">JsonInclude</span>]</mark>
<span>        <span class="hljs-keyword">public</span> DateTime Date;</span>
<mark>        [<span class="hljs-meta">JsonInclude</span>]</mark>
<span>        <span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> TemperatureC;</span>
<mark>        [<span class="hljs-meta">JsonInclude</span>]</mark>
<span>        <span class="hljs-keyword">public</span> <span class="hljs-keyword">string</span> Summary;
    }

    <span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title">Program</span>
    {
        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">Main</span>(<span class="hljs-params"></span>)</span>
        {
            <span class="hljs-keyword">var</span> json =
                <span class="hljs-string">@"{""Date"":""2020-09-06T11:31:01.923395"",""TemperatureC"":-1,""Summary"":""Cold""} "</span>;
            Console.WriteLine(<span class="hljs-string">$"Input JSON: <span class="hljs-subst">{json}</span>"</span>);
</span>
<mark>            <span class="hljs-keyword">var</span> options = <span class="hljs-keyword">new</span> JsonSerializerOptions
            {
                IncludeFields = <span class="hljs-literal">true</span>,
            };</mark>
<span>            <span class="hljs-keyword">var</span> forecast = JsonSerializer.Deserialize&lt;Forecast&gt;(json, options);

            Console.WriteLine(<span class="hljs-string">$"forecast.Date: <span class="hljs-subst">{forecast.Date}</span>"</span>);
            Console.WriteLine(<span class="hljs-string">$"forecast.TemperatureC: <span class="hljs-subst">{forecast.TemperatureC}</span>"</span>);
            Console.WriteLine(<span class="hljs-string">$"forecast.Summary: <span class="hljs-subst">{forecast.Summary}</span>"</span>);

            <span class="hljs-keyword">var</span> roundTrippedJson =
                JsonSerializer.Serialize&lt;Forecast&gt;(forecast, options);

            Console.WriteLine(<span class="hljs-string">$"Output JSON: <span class="hljs-subst">{roundTrippedJson}</span>"</span>);

            <span class="hljs-keyword">var</span> forecast2 = JsonSerializer.Deserialize&lt;Forecast2&gt;(json);

            Console.WriteLine(<span class="hljs-string">$"forecast2.Date: <span class="hljs-subst">{forecast2.Date}</span>"</span>);
            Console.WriteLine(<span class="hljs-string">$"forecast2.TemperatureC: <span class="hljs-subst">{forecast2.TemperatureC}</span>"</span>);
            Console.WriteLine(<span class="hljs-string">$"forecast2.Summary: <span class="hljs-subst">{forecast2.Summary}</span>"</span>);

            roundTrippedJson = JsonSerializer.Serialize&lt;Forecast2&gt;(forecast2);
            
            Console.WriteLine(<span class="hljs-string">$"Output JSON: <span class="hljs-subst">{roundTrippedJson}</span>"</span>);
        }
    }
}

<span class="hljs-comment">// Produces output like the following example:</span>
<span class="hljs-comment">//</span>
<span class="hljs-comment">//Input JSON: { "Date":"2020-09-06T11:31:01.923395","TemperatureC":-1,"Summary":"Cold"}</span>
<span class="hljs-comment">//forecast.Date: 9/6/2020 11:31:01 AM</span>
<span class="hljs-comment">//forecast.TemperatureC: -1</span>
<span class="hljs-comment">//forecast.Summary: Cold</span>
<span class="hljs-comment">//Output JSON: { "Date":"2020-09-06T11:31:01.923395","TemperatureC":-1,"Summary":"Cold"}</span>
<span class="hljs-comment">//forecast2.Date: 9/6/2020 11:31:01 AM</span>
<span class="hljs-comment">//forecast2.TemperatureC: -1</span>
<span class="hljs-comment">//forecast2.Summary: Cold</span>
<span class="hljs-comment">//Output JSON: { "Date":"2020-09-06T11:31:01.923395","TemperatureC":-1,"Summary":"Cold"}</span>
</span></code></pre>
<p><span data-ttu-id="dd9e3-217">若要忽略只读字段，请使用 <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.text.json.jsonserializeroptions.ignorereadonlyfields" data-linktype="absolute-path">JsonSerializerOptions.IgnoreReadOnlyFields</a> 全局设置。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-217">To ignore read-only fields, use the <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.text.json.jsonserializeroptions.ignorereadonlyfields" data-linktype="absolute-path">JsonSerializerOptions.IgnoreReadOnlyFields</a> global setting.</span></span></p>
</div>
<div class="zone has-pivot" data-pivot="dotnet-core-3-1">
<p><span data-ttu-id="dd9e3-218">.NET Core 3.1 的 System.Text.Json 中不支持字段。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-218">Fields are not supported in System.Text.Json in .NET Core 3.1.</span></span> <span data-ttu-id="dd9e3-219"><a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-converters-how-to" data-linktype="relative-path">自定义转换器</a>可提供此功能。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-219"><a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-converters-how-to" data-linktype="relative-path">Custom converters</a> can provide this functionality.</span></span></p>
</div>
<h2 id="httpclient-and-httpcontent-extension-methods" class="heading-anchor"><a class="anchor-link docon docon-link" href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-5-0#httpclient-and-httpcontent-extension-methods" aria-labelledby="httpclient-and-httpcontent-extension-methods"></a><span data-ttu-id="dd9e3-220">HttpClient 和 HttpContent 扩展方法</span><span class="sxs-lookup"><span data-stu-id="dd9e3-220">HttpClient and HttpContent extension methods</span></span></h2>
<div class="zone has-pivot" data-pivot="dotnet-5-0">
<p><span data-ttu-id="dd9e3-221">序列化和反序列化来自网络的 JSON 有效负载是常见的操作。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-221">Serializing and deserializing JSON payloads from the network are common operations.</span></span> <span data-ttu-id="dd9e3-222"><a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.net.http.json.httpclientjsonextensions" data-linktype="absolute-path">HttpClient</a> 和 <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.net.http.json.httpcontentjsonextensions" data-linktype="absolute-path">HttpContent</a> 上的扩展方法允许在单个代码行中执行这些操作。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-222">Extension methods on <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.net.http.json.httpclientjsonextensions" data-linktype="absolute-path">HttpClient</a> and <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.net.http.json.httpcontentjsonextensions" data-linktype="absolute-path">HttpContent</a> let you do these operations in a single line of code.</span></span> <span data-ttu-id="dd9e3-223">这些扩展方法使用 <a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-configure-options#web-defaults-for-jsonserializeroptions" data-linktype="relative-path">JsonSerializerOptions 的 Web 默认值</a>。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-223">These extension methods use <a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-configure-options#web-defaults-for-jsonserializeroptions" data-linktype="relative-path">web defaults for JsonSerializerOptions</a>.</span></span></p>
<p><span data-ttu-id="dd9e3-224">以下示例说明了 <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.net.http.json.httpclientjsonextensions.getfromjsonasync" data-linktype="absolute-path">HttpClientJsonExtensions.GetFromJsonAsync</a> 和 <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.net.http.json.httpclientjsonextensions.postasjsonasync" data-linktype="absolute-path">HttpClientJsonExtensions.PostAsJsonAsync</a> 的用法：</span><span class="sxs-lookup"><span data-stu-id="dd9e3-224">The following example illustrates use of <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.net.http.json.httpclientjsonextensions.getfromjsonasync" data-linktype="absolute-path">HttpClientJsonExtensions.GetFromJsonAsync</a> and <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.net.http.json.httpclientjsonextensions.postasjsonasync" data-linktype="absolute-path">HttpClientJsonExtensions.PostAsJsonAsync</a>:</span></span></p>
<div class="codeHeader" id="code-try-19" data-bi-name="code-header"><span class="language">C#</span><button type="button" class="action is-relative" data-bi-name="copy" aria-label="复制代码">
				<span class="icon">
					<span class="docon docon-edit-copy" role="presentation"></span>
				</span>
				<span>复制</span>
				<div class="successful-copy-alert is-absolute has-right-zero has-top-zero has-left-zero has-bottom-zero is-flex has-flex-align-items-center has-flex-justify-content-center has-text-success-invert has-background-success is-transparent" aria-hidden="true">
					<span class="icon is-size-large">
						<span class="docon docon-check-mark" role="presentation"></span>
					</span>
				</div>
			</button></div><pre tabindex="0" class="has-inner-focus"><code class="lang-csharp" highlight-lines="26,33" data-author-content="using System;
using System.Net.Http;
using System.Net.Http.Json;
using System.Threading.Tasks;

namespace HttpClientExtensionMethods
{
    public class User
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Username { get; set; }
        public string Email { get; set; }
    }

    public class Program
    {
        public static async Task Main()
        {
            using HttpClient client = new()
            {
                BaseAddress = new Uri(&quot;https://jsonplaceholder.typicode.com&quot;)
            };

            // Get the user information.
            User user = await client.GetFromJsonAsync&lt;User&gt;(&quot;users/1&quot;);
            Console.WriteLine($&quot;Id: {user.Id}&quot;);
            Console.WriteLine($&quot;Name: {user.Name}&quot;);
            Console.WriteLine($&quot;Username: {user.Username}&quot;);
            Console.WriteLine($&quot;Email: {user.Email}&quot;);

            // Post a new user.
            HttpResponseMessage response = await client.PostAsJsonAsync(&quot;users&quot;, user);
            Console.WriteLine(
                $&quot;{(response.IsSuccessStatusCode ? &quot;Success&quot; : &quot;Error&quot;)} - {response.StatusCode}&quot;);
        }
    }
}

// Produces output like the following example but with different names:
//
//Id: 1
//Name: Tyler King
//Username: Tyler
//Email: Tyler @contoso.com
//Success - Created
"><span><span class="hljs-keyword">using</span> System;
<span class="hljs-keyword">using</span> System.Net.Http;
<span class="hljs-keyword">using</span> System.Net.Http.Json;
<span class="hljs-keyword">using</span> System.Threading.Tasks;

<span class="hljs-keyword">namespace</span> <span class="hljs-title">HttpClientExtensionMethods</span>
{
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title">User</span>
    {
        <span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> Id { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }
        <span class="hljs-keyword">public</span> <span class="hljs-keyword">string</span> Name { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }
        <span class="hljs-keyword">public</span> <span class="hljs-keyword">string</span> Username { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }
        <span class="hljs-keyword">public</span> <span class="hljs-keyword">string</span> Email { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }
    }

    <span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title">Program</span>
    {
        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">async</span> Task <span class="hljs-title">Main</span>(<span class="hljs-params"></span>)</span>
        {
            <span class="hljs-keyword">using</span> HttpClient client = <span class="hljs-keyword">new</span>()
            {
                BaseAddress = <span class="hljs-keyword">new</span> Uri(<span class="hljs-string">"https://jsonplaceholder.typicode.com"</span>)
            };

            <span class="hljs-comment">// Get the user information.</span></span>
<mark>            User user = <span class="hljs-keyword">await</span> client.GetFromJsonAsync&lt;User&gt;(<span class="hljs-string">"users/1"</span>);</mark>
<span>            Console.WriteLine(<span class="hljs-string">$"Id: <span class="hljs-subst">{user.Id}</span>"</span>);
            Console.WriteLine(<span class="hljs-string">$"Name: <span class="hljs-subst">{user.Name}</span>"</span>);
            Console.WriteLine(<span class="hljs-string">$"Username: <span class="hljs-subst">{user.Username}</span>"</span>);
            Console.WriteLine(<span class="hljs-string">$"Email: <span class="hljs-subst">{user.Email}</span>"</span>);

            <span class="hljs-comment">// Post a new user.</span></span>
<mark>            HttpResponseMessage response = <span class="hljs-keyword">await</span> client.PostAsJsonAsync(<span class="hljs-string">"users"</span>, user);</mark>
<span>            Console.WriteLine(
                <span class="hljs-string">$"<span class="hljs-subst">{(response.IsSuccessStatusCode ? <span class="hljs-string">"Success"</span> : <span class="hljs-string">"Error"</span>)}</span> - <span class="hljs-subst">{response.StatusCode}</span>"</span>);
        }
    }
}

<span class="hljs-comment">// Produces output like the following example but with different names:</span>
<span class="hljs-comment">//</span>
<span class="hljs-comment">//Id: 1</span>
<span class="hljs-comment">//Name: Tyler King</span>
<span class="hljs-comment">//Username: Tyler</span>
<span class="hljs-comment">//Email: Tyler @contoso.com</span>
<span class="hljs-comment">//Success - Created</span>
</span></code></pre>
<p><span data-ttu-id="dd9e3-225">此外还有 <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.net.http.json.httpcontentjsonextensions" data-linktype="absolute-path">HttpContent</a> 上的 System.Text.Json 的扩展方法。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-225">There are also extension methods for System.Text.Json on <a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.net.http.json.httpcontentjsonextensions" data-linktype="absolute-path">HttpContent</a>.</span></span></p>
</div>
<div class="zone has-pivot" data-pivot="dotnet-core-3-1">
<p><span data-ttu-id="dd9e3-226"><code>HttpClient</code> 和 <code>HttpContent</code> 上的扩展方法在 .NET Core 3.1 的 System.Text.Json 中不可用。</span><span class="sxs-lookup"><span data-stu-id="dd9e3-226">Extension methods on <code>HttpClient</code> and <code>HttpContent</code> are not available in System.Text.Json in .NET Core 3.1.</span></span></p>
</div>
<h2 id="see-also" class="heading-anchor"><a class="anchor-link docon docon-link" href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-5-0#see-also" aria-labelledby="see-also"></a><span data-ttu-id="dd9e3-227">请参阅</span><span class="sxs-lookup"><span data-stu-id="dd9e3-227">See also</span></span></h2>
<ul>
<li><a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-overview" data-linktype="relative-path"><span data-ttu-id="dd9e3-228">System.Text.Json 概述</span><span class="sxs-lookup"><span data-stu-id="dd9e3-228">System.Text.Json overview</span></span></a></li>
<li><a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-configure-options" data-linktype="relative-path"><span data-ttu-id="dd9e3-229">对 JsonSerializerOptions 实例进行实例化</span><span class="sxs-lookup"><span data-stu-id="dd9e3-229">Instantiate JsonSerializerOptions instances</span></span></a></li>
<li><a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-character-casing" data-linktype="relative-path"><span data-ttu-id="dd9e3-230">启用不区分大小写的匹配</span><span class="sxs-lookup"><span data-stu-id="dd9e3-230">Enable case-insensitive matching</span></span></a></li>
<li><a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-customize-properties" data-linktype="relative-path"><span data-ttu-id="dd9e3-231">自定义属性名称和值</span><span class="sxs-lookup"><span data-stu-id="dd9e3-231">Customize property names and values</span></span></a></li>
<li><a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-ignore-properties" data-linktype="relative-path"><span data-ttu-id="dd9e3-232">忽略属性</span><span class="sxs-lookup"><span data-stu-id="dd9e3-232">Ignore properties</span></span></a></li>
<li><a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-invalid-json" data-linktype="relative-path"><span data-ttu-id="dd9e3-233">允许无效的 JSON</span><span class="sxs-lookup"><span data-stu-id="dd9e3-233">Allow invalid JSON</span></span></a></li>
<li><a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-handle-overflow" data-linktype="relative-path"><span data-ttu-id="dd9e3-234">处理溢出 JSON</span><span class="sxs-lookup"><span data-stu-id="dd9e3-234">Handle overflow JSON</span></span></a></li>
<li><a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-preserve-references" data-linktype="relative-path"><span data-ttu-id="dd9e3-235">保留引用</span><span class="sxs-lookup"><span data-stu-id="dd9e3-235">Preserve references</span></span></a></li>
<li><a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-immutability" data-linktype="relative-path"><span data-ttu-id="dd9e3-236">不可变类型和非公共访问器</span><span class="sxs-lookup"><span data-stu-id="dd9e3-236">Immutable types and non-public accessors</span></span></a></li>
<li><a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-polymorphism" data-linktype="relative-path"><span data-ttu-id="dd9e3-237">多态序列化</span><span class="sxs-lookup"><span data-stu-id="dd9e3-237">Polymorphic serialization</span></span></a></li>
<li><a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-migrate-from-newtonsoft-how-to" data-linktype="relative-path"><span data-ttu-id="dd9e3-238">从 Newtonsoft.Json 迁移到 System.Text.Json</span><span class="sxs-lookup"><span data-stu-id="dd9e3-238">Migrate from Newtonsoft.Json to System.Text.Json</span></span></a></li>
<li><a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-character-encoding" data-linktype="relative-path"><span data-ttu-id="dd9e3-239">自定义字符编码</span><span class="sxs-lookup"><span data-stu-id="dd9e3-239">Customize character encoding</span></span></a></li>
<li><a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/write-custom-serializer-deserializer" data-linktype="relative-path"><span data-ttu-id="dd9e3-240">编写自定义序列化程序和反序列化程序</span><span class="sxs-lookup"><span data-stu-id="dd9e3-240">Write custom serializers and deserializers</span></span></a></li>
<li><a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-converters-how-to" data-linktype="relative-path"><span data-ttu-id="dd9e3-241">编写用于 JSON 序列化的自定义转换器</span><span class="sxs-lookup"><span data-stu-id="dd9e3-241">Write custom converters for JSON serialization</span></span></a></li>
<li><a href="https://docs.microsoft.com/zh-cn/dotnet/standard/datetime/system-text-json-support" data-linktype="relative-path"><span data-ttu-id="dd9e3-242">DateTime 和 DateTimeOffset 支持</span><span class="sxs-lookup"><span data-stu-id="dd9e3-242">DateTime and DateTimeOffset support</span></span></a></li>
<li><span data-ttu-id="dd9e3-243"><a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.text.json" data-linktype="absolute-path">System.Text.Json API 参考</a></span><span class="sxs-lookup"><span data-stu-id="dd9e3-243"><a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.text.json" data-linktype="absolute-path">System.Text.Json API reference</a></span></span></li>
<li><span data-ttu-id="dd9e3-244"><a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.text.json.serialization" data-linktype="absolute-path">System.Text.Json.Serialization API 参考</a></span><span class="sxs-lookup"><span data-stu-id="dd9e3-244"><a href="https://docs.microsoft.com/zh-cn/dotnet/api/system.text.json.serialization" data-linktype="absolute-path">System.Text.Json.Serialization API reference</a></span></span></li>
</ul>

						<!-- </content> -->

						</main>

						<!-- page rating section -->
								<div class="is-hidden-desktop has-border-top has-margin-top-large has-padding-top-large binary-rating-holder">
									
	
	

	<div class="feedback-verbatim has-border-bottom has-padding-bottom-large has-margin-bottom-small" data-bi-name="rating">
    <div class="binary-rating">
        <div class="binary-rating-buttons">
				<h3 class="has-text-weight-semibold has-margin-top-none has-margin-bottom-small is-size-h4 has-margin-bottom-medium">此页面有帮助吗?</h3>
			<div>
                <button class="thumb-rating like has-inner-focus" title="是" data-bi-name="rating-yes" aria-expanded="false" data-bi-sat="1" aria-controls="rating-container-desktop">
                    <span aria-hidden="true" class="icon docon docon-like"></span>
                    <span>是</span>
                </button>
                <button class="thumb-rating dislike has-inner-focus" title="否" data-bi-name="rating-no" data-bi-sat="0" aria-expanded="false" aria-controls="rating-container-desktop">
                    <span aria-hidden="true" class="icon docon docon-dislike"></span>
                    <span>否</span>
                </button>
            </div>
        </div>
        <form class="feedback-verbatim-form is-hidden" id="rating-container-desktop">
            <div class="verbatim-textarea box is-relative has-box-shadow-none has-border has-margin-top-small has-padding-extra-small is-size-extra-small">
                <label for="rating-textarea-desktop" class="visually-hidden">还有其他反馈吗？</label>
                <textarea id="rating-textarea-desktop" rows="4" maxlength="999" placeholder="还有其他反馈吗？" required="" class="textarea has-border-none has-box-shadow-none has-inner-focus"></textarea>
            </div>
            <div class="buttons is-right has-margin-top-medium has-margin-right-extra-small">
                <button class="skip-rating button is-transparent has-text-primary is-small has-border-none" type="button">跳过</button>
                <button class="submit-rating button is-primary is-small" data-bi-name="rating-verbatim" disabled="" type="submit">提交</button>
            </div>
        </form>
    </div>
    <div class="thankyou-rating is-hidden" tabindex="-1">
        <p>谢谢。</p>
    </div>
</div>									</div>
						<!-- end page rating section -->


						<!-- recommendations section -->
						<!-- end recommendations section -->

						<!-- feedback section -->
<section class="feedback-section is-relative" data-bi-name="feedback-section">

    <h2 id="feedback" class="title is-3 has-margin-top-large">反馈</h2>

    <div class="alert choose-feedback-type">
        <p aria-hidden="true" id="send-feedback-about">提交和查看相关反馈</p>

        <div class="choose-feedback-buttons has-margin-top-medium">
                <a class="button has-external-link-indicator feedback-type-product has-margin-bottom-small" aria-label="发送有关此产品的反馈" href="https://aka.ms/feedback/report?space=61" data-bi-name="product-feedback">
                    <span>此产品</span>
                </a>

			<a class="button feedback-type-product has-margin-bottom-small github-link" aria-label="发送有关此页面的反馈" data-bi-name="create-issue-on-github" href="https://github.com/dotnet/docs.zh-cn/issues/new?title=&amp;body=%0A%0A%5B%E5%9C%A8%E6%AD%A4%E5%A4%84%E8%BE%93%E5%85%A5%E5%8F%8D%E9%A6%88%5D%0A%0A%0A---%0A%23%23%23%23%20%E6%96%87%E6%A1%A3%E8%AF%A6%E7%BB%86%E4%BF%A1%E6%81%AF%0A%0A%E2%9A%A0%20*%E8%AF%B7%E5%8B%BF%E7%BC%96%E8%BE%91%E6%AD%A4%E9%83%A8%E5%88%86%E3%80%82%20docs.microsoft.com%20%E2%9E%9F%20GitHub%20%E9%97%AE%E9%A2%98%E9%93%BE%E6%8E%A5%E5%BF%85%E9%A1%BB%E5%85%B7%E6%9C%89%E6%AD%A4%E9%83%A8%E5%88%86%E3%80%82*%0A%0A*%20ID%3A%20588ae32f-3429-143f-824c-5ab2d046d24a%0A*%20Version%20Independent%20ID%3A%20002f9f2a-48d1-0671-a079-b99b9f6bab58%0A*%20Content%3A%20%5B%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8%20C%23%20%E5%AF%B9%20JSON%20%E8%BF%9B%E8%A1%8C%E5%BA%8F%E5%88%97%E5%8C%96%E5%92%8C%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96%20-%20.NET%5D(https%3A%2F%2Fdocs.microsoft.com%2Fzh-cn%2Fdotnet%2Fstandard%2Fserialization%2Fsystem-text-json-how-to%3Fpivots%3Ddotnet-5-0)%0A*%20Content%20Source%3A%20%5Bdocs%2Fstandard%2Fserialization%2Fsystem-text-json-how-to.md%5D(https%3A%2F%2Fgithub.com%2Fdotnet%2Fdocs.zh-cn%2Fblob%2Flive%2Fdocs%2Fstandard%2Fserialization%2Fsystem-text-json-how-to.md)%0A*%20Product%3A%20**dotnet-fundamentals**%0A*%20GitHub%20Login%3A%20%40tdykstra%0A*%20Microsoft%20Alias%3A%20**tdykstra**">
				<span aria-hidden="true" class="docon docon-brand-github has-padding-right-extra-small"></span>
				<span>此页面</span>
			</a>
        </div>
    </div>

    <div class="action-container is-flex has-flex-justify-content-end has-margin-top-small has-margin-bottom-small">
        <a class="view-on-github has-external-link-indicator" data-bi-name="view-on-github" href="https://github.com/dotnet/docs.zh-cn/issues?utf8=%E2%9C%93&amp;q=%22002f9f2a-48d1-0671-a079-b99b9f6bab58%22&amp;in=body">
            <span aria-hidden="true" class="docon docon-brand-github"></span>
            <span>查看所有页面反馈</span>
        </a>
    </div>
</section>
						<!-- end feedback section -->

						<!-- feedback report section -->
						<!-- end feedback report section -->

						<div class="footerContainer is-visible-interactive has-default-focus ">



	<footer id="footer-interactive" data-bi-name="footer" class="footer-layout">

    <div class="is-flex is-full-height has-padding-right-extra-large-desktop">
			<a data-mscc-ic="false" class="locale-selector-link has-flex-shrink-none" href="https://docs.microsoft.com/zh-cn/locale?target=https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-5-0" data-bi-name="select-locale"><span class="icon docon docon-world is-size-large has-margin-right-small" aria-hidden="true"></span><span class="local-selector-link-text">中文 (简体)</span></a>
		<div class="has-margin-left-medium has-margin-right-medium has-flex-shrink-none">
<div class="dropdown has-caret-up">
	<button class="dropdown-trigger button is-transparent is-small is-icon-only-touch has-inner-focus theme-dropdown-trigger" aria-controls="theme-menu-interactive" aria-expanded="false" title="主题" data-bi-name="theme">
		<span class="icon">
			<span class="docon docon-sun" aria-hidden="true"></span>
		</span>
		<span>主题</span>
	</button>
	<div class="dropdown-menu" id="theme-menu-interactive" role="menu">
		<ul class="theme-selector has-padding-small">
			<li class="theme is-block">
				<button class="button is-text is-small theme-control is-fullwidth has-flex-justify-content-start is-selected" data-theme-to="light" aria-pressed="true">
					<span class="theme-light has-margin-right-small">
						<span class="theme-selector-icon css-variable-support has-border is-inline-block has-body-background" aria-hidden="true">
							<svg class="svg" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 22 14">
								<rect width="22" height="14" class="has-fill-body-background"></rect>
								<rect x="5" y="5" width="12" height="4" class="has-fill-secondary"></rect>
								<rect x="5" y="2" width="2" height="1" class="has-fill-secondary"></rect>
								<rect x="8" y="2" width="2" height="1" class="has-fill-secondary"></rect>
								<rect x="11" y="2" width="3" height="1" class="has-fill-secondary"></rect>
								<rect x="1" y="1" width="2" height="2" class="has-fill-secondary"></rect>
								<rect x="5" y="10" width="7" height="2" rx="0.3" class="has-fill-primary"></rect>
								<rect x="19" y="1" width="2" height="2" rx="1" class="has-fill-secondary"></rect>
							</svg>
						</span>
					</span>
					<span role="menuitem">
亮					</span>
				</button>
			</li>
			<li class="theme is-block">
				<button class="button is-text is-small theme-control is-fullwidth has-flex-justify-content-start" data-theme-to="dark" aria-pressed="false">
					<span class="theme-dark has-margin-right-small">
						<span class="has-border theme-selector-icon css-variable-support is-inline-block has-body-background" aria-hidden="true">
							<svg class="svg" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 22 14">
								<rect width="22" height="14" class="has-fill-body-background"></rect>
								<rect x="5" y="5" width="12" height="4" class="has-fill-secondary"></rect>
								<rect x="5" y="2" width="2" height="1" class="has-fill-secondary"></rect>
								<rect x="8" y="2" width="2" height="1" class="has-fill-secondary"></rect>
								<rect x="11" y="2" width="3" height="1" class="has-fill-secondary"></rect>
								<rect x="1" y="1" width="2" height="2" class="has-fill-secondary"></rect>
								<rect x="5" y="10" width="7" height="2" rx="0.3" class="has-fill-primary"></rect>
								<rect x="19" y="1" width="2" height="2" rx="1" class="has-fill-secondary"></rect>
							</svg>
						</span>
					</span>
					<span role="menuitem">
暗					</span>
				</button>
			</li>
			<li class="theme is-block">
				<button class="button is-text is-small theme-control is-fullwidth has-flex-justify-content-start" data-theme-to="high-contrast" aria-pressed="false">
					<span class="theme-high-contrast has-margin-right-small">
						<span class="has-border theme-selector-icon css-variable-support is-inline-block has-body-background" aria-hidden="true">
							<svg class="svg" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 22 14">
								<rect width="22" height="14" class="has-fill-body-background"></rect>
								<rect x="5" y="5" width="12" height="4" class="has-fill-secondary"></rect>
								<rect x="5" y="2" width="2" height="1" class="has-fill-secondary"></rect>
								<rect x="8" y="2" width="2" height="1" class="has-fill-secondary"></rect>
								<rect x="11" y="2" width="3" height="1" class="has-fill-secondary"></rect>
								<rect x="1" y="1" width="2" height="2" class="has-fill-secondary"></rect>
								<rect x="5" y="10" width="7" height="2" rx="0.3" class="has-fill-primary"></rect>
								<rect x="19" y="1" width="2" height="2" rx="1" class="has-fill-secondary"></rect>
							</svg>
						</span>
					</span>
					<span role="menuitem">
高对比度					</span>
				</button>
			</li>
		</ul>
	</div>
</div>		</div>
	</div>
    <ul class="links" data-bi-name="footerlinks">
		<li class="manage-cookies-holder" hidden=""></li>
			<li><a data-mscc-ic="false" href="https://docs.microsoft.com/zh-cn/previous-versions/" data-bi-name="archivelink">旧版文档</a></li>
			<li><a data-mscc-ic="false" href="https://docs.microsoft.com/zh-cn/teamblog" data-bi-name="bloglink">博客</a></li>
			<li><a data-mscc-ic="false" href="https://docs.microsoft.com/zh-cn/contribute" data-bi-name="contributorGuide">参与</a></li>
				<li><a data-mscc-ic="false" href="https://go.microsoft.com/fwlink/?LinkId=521839" data-bi-name="privacy">隐私和 Cookie</a></li>
			<li><a data-mscc-ic="false" href="https://docs.microsoft.com/zh-cn/legal/termsofuse" data-bi-name="termsofuse">使用条款</a></li>
				<li><a data-mscc-ic="false" href="https://aka.ms/sitefeedback" data-bi-name="feedback">网站反馈</a></li>
			<li><a data-mscc-ic="false" href="https://www.microsoft.com/en-us/legal/intellectualproperty/Trademarks/EN-US.aspx" data-bi-name="trademarks">商标</a></li>
		<li>© Microsoft 2021</li>
    </ul>
</footer>
						</div>
					</div>

					<div class="is-size-small right-container column is-one-quarter is-one-fifth-desktop is-hidden-mobile is-hidden-tablet-only" data-bi-name="pageactions" role="complementary" aria-label="页面操作">
						<div id="affixed-right-container" class="doc-outline is-fixed is-vertically-scrollable" style="width: 172.938px; top: 66px; bottom: 24px;">
								
	
	

	<div class="feedback-verbatim has-border-bottom has-padding-bottom-small has-margin-bottom-small" data-bi-name="rating">
    <div class="binary-rating">
        <div class="binary-rating-buttons">

				<h3 class="has-text-weight-semibold has-margin-top-none has-margin-bottom-small">此页面有帮助吗?</h3>
			<div>
                <button class="thumb-rating like has-inner-focus" title="是" data-bi-name="rating-yes" aria-expanded="false" data-bi-sat="1" aria-controls="rating-container-mobile">
                    <span aria-hidden="true" class="icon docon docon-like"></span>
                    <span>是</span>
                </button>
                <button class="thumb-rating dislike has-inner-focus" title="否" data-bi-name="rating-no" data-bi-sat="0" aria-expanded="false" aria-controls="rating-container-mobile">
                    <span aria-hidden="true" class="icon docon docon-dislike"></span>
                    <span>否</span>
                </button>
            </div>
        </div>
        <form class="feedback-verbatim-form is-hidden" id="rating-container-mobile">
            <div class="verbatim-textarea box is-relative has-box-shadow-none has-border has-margin-top-small has-padding-extra-small is-size-extra-small">
                <label for="rating-textarea-mobile" class="visually-hidden">还有其他反馈吗？</label>
                <textarea id="rating-textarea-mobile" rows="4" maxlength="999" placeholder="还有其他反馈吗？" required="" class="textarea has-border-none has-box-shadow-none has-inner-focus"></textarea>
            </div>
            <div class="buttons is-right has-margin-top-medium has-margin-right-extra-small">
                <button class="skip-rating button is-transparent has-text-primary is-small has-border-none" type="button">跳过</button>
                <button class="submit-rating button is-primary is-small" data-bi-name="rating-verbatim" disabled="" type="submit">提交</button>
            </div>
        </form>
    </div>
    <div class="thankyou-rating is-hidden" tabindex="-1">
        <p>谢谢。</p>
    </div>
</div>							<nav id="side-doc-outline" data-bi-name="intopic toc" role="navigation" aria-label="文章大纲">
								<h3>本文内容</h3>
							<ol><li class="selected"><a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-5-0#namespaces">命名空间</a></li><li class=""><a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-5-0#how-to-write-net-objects-as-json-serialize">如何将 .NET 对象编写为 JSON（序列化）</a></li><li><a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-5-0#serialize-to-utf-8">序列化为 UTF-8</a></li><li><a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-5-0#serialization-behavior">序列化行为</a></li><li><a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-5-0#how-to-read-json-as-net-objects-deserialize">如何将 JSON 读取为 .NET 对象（反序列化）</a></li><li><a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-5-0#deserialize-from-utf-8">从 UTF-8 进行反序列化</a></li><li><a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-5-0#deserialization-behavior">反序列化行为</a></li><li><a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-5-0#serialize-to-formatted-json">序列化为格式化 JSON</a></li><li><a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-5-0#include-fields">包含字段</a></li><li><a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-5-0#httpclient-and-httpcontent-extension-methods">HttpClient 和 HttpContent 扩展方法</a></li><li><a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-5-0#see-also">请参阅</a></li></ol></nav>
						</div>
					</div>

					<!--end of div.columns -->
				</div>

			<!--end of .primary-holder -->
			</section>

			<aside id="interactive-container" class="interactive-container is-visible-interactive column has-body-background-dark ">
			</aside>
		</div>

		<!--end of .mainContainer -->
	</div>

	<div class="footerContainer has-default-focus is-hidden-interactive ">



	<footer id="footer" data-bi-name="footer" class="footer-layout uhf-container has-padding" role="contentinfo">

    <div class="is-flex is-full-height has-padding-right-extra-large-desktop">
			<a data-mscc-ic="false" class="locale-selector-link has-flex-shrink-none" href="https://docs.microsoft.com/zh-cn/locale?target=https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-5-0" data-bi-name="select-locale"><span class="icon docon docon-world is-size-large has-margin-right-small" aria-hidden="true"></span><span class="local-selector-link-text">中文 (简体)</span></a>
		<div class="has-margin-left-medium has-margin-right-medium has-flex-shrink-none">
<div class="dropdown has-caret-up">
	<button class="dropdown-trigger button is-transparent is-small is-icon-only-touch has-inner-focus theme-dropdown-trigger" aria-controls="theme-menu" aria-expanded="false" title="主题" data-bi-name="theme">
		<span class="icon">
			<span class="docon docon-sun" aria-hidden="true"></span>
		</span>
		<span>主题</span>
	</button>
	<div class="dropdown-menu" id="theme-menu" role="menu">
		<ul class="theme-selector has-padding-small">
			<li class="theme is-block">
				<button class="button is-text is-small theme-control is-fullwidth has-flex-justify-content-start is-selected" data-theme-to="light" aria-pressed="true">
					<span class="theme-light has-margin-right-small">
						<span class="theme-selector-icon css-variable-support has-border is-inline-block has-body-background" aria-hidden="true">
							<svg class="svg" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 22 14">
								<rect width="22" height="14" class="has-fill-body-background"></rect>
								<rect x="5" y="5" width="12" height="4" class="has-fill-secondary"></rect>
								<rect x="5" y="2" width="2" height="1" class="has-fill-secondary"></rect>
								<rect x="8" y="2" width="2" height="1" class="has-fill-secondary"></rect>
								<rect x="11" y="2" width="3" height="1" class="has-fill-secondary"></rect>
								<rect x="1" y="1" width="2" height="2" class="has-fill-secondary"></rect>
								<rect x="5" y="10" width="7" height="2" rx="0.3" class="has-fill-primary"></rect>
								<rect x="19" y="1" width="2" height="2" rx="1" class="has-fill-secondary"></rect>
							</svg>
						</span>
					</span>
					<span role="menuitem">
亮					</span>
				</button>
			</li>
			<li class="theme is-block">
				<button class="button is-text is-small theme-control is-fullwidth has-flex-justify-content-start" data-theme-to="dark" aria-pressed="false">
					<span class="theme-dark has-margin-right-small">
						<span class="has-border theme-selector-icon css-variable-support is-inline-block has-body-background" aria-hidden="true">
							<svg class="svg" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 22 14">
								<rect width="22" height="14" class="has-fill-body-background"></rect>
								<rect x="5" y="5" width="12" height="4" class="has-fill-secondary"></rect>
								<rect x="5" y="2" width="2" height="1" class="has-fill-secondary"></rect>
								<rect x="8" y="2" width="2" height="1" class="has-fill-secondary"></rect>
								<rect x="11" y="2" width="3" height="1" class="has-fill-secondary"></rect>
								<rect x="1" y="1" width="2" height="2" class="has-fill-secondary"></rect>
								<rect x="5" y="10" width="7" height="2" rx="0.3" class="has-fill-primary"></rect>
								<rect x="19" y="1" width="2" height="2" rx="1" class="has-fill-secondary"></rect>
							</svg>
						</span>
					</span>
					<span role="menuitem">
暗					</span>
				</button>
			</li>
			<li class="theme is-block">
				<button class="button is-text is-small theme-control is-fullwidth has-flex-justify-content-start" data-theme-to="high-contrast" aria-pressed="false">
					<span class="theme-high-contrast has-margin-right-small">
						<span class="has-border theme-selector-icon css-variable-support is-inline-block has-body-background" aria-hidden="true">
							<svg class="svg" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 22 14">
								<rect width="22" height="14" class="has-fill-body-background"></rect>
								<rect x="5" y="5" width="12" height="4" class="has-fill-secondary"></rect>
								<rect x="5" y="2" width="2" height="1" class="has-fill-secondary"></rect>
								<rect x="8" y="2" width="2" height="1" class="has-fill-secondary"></rect>
								<rect x="11" y="2" width="3" height="1" class="has-fill-secondary"></rect>
								<rect x="1" y="1" width="2" height="2" class="has-fill-secondary"></rect>
								<rect x="5" y="10" width="7" height="2" rx="0.3" class="has-fill-primary"></rect>
								<rect x="19" y="1" width="2" height="2" rx="1" class="has-fill-secondary"></rect>
							</svg>
						</span>
					</span>
					<span role="menuitem">
高对比度					</span>
				</button>
			</li>
		</ul>
	</div>
</div>		</div>
	</div>
    <ul class="links" data-bi-name="footerlinks">
		<li class="manage-cookies-holder" hidden=""></li>
			<li><a data-mscc-ic="false" href="https://docs.microsoft.com/zh-cn/previous-versions/" data-bi-name="archivelink">旧版文档</a></li>
			<li><a data-mscc-ic="false" href="https://docs.microsoft.com/zh-cn/teamblog" data-bi-name="bloglink">博客</a></li>
			<li><a data-mscc-ic="false" href="https://docs.microsoft.com/zh-cn/contribute" data-bi-name="contributorGuide">参与</a></li>
				<li><a data-mscc-ic="false" href="https://go.microsoft.com/fwlink/?LinkId=521839" data-bi-name="privacy">隐私和 Cookie</a></li>
			<li><a data-mscc-ic="false" href="https://docs.microsoft.com/zh-cn/legal/termsofuse" data-bi-name="termsofuse">使用条款</a></li>
				<li><a data-mscc-ic="false" href="https://aka.ms/sitefeedback" data-bi-name="feedback">网站反馈</a></li>
			<li><a data-mscc-ic="false" href="https://www.microsoft.com/en-us/legal/intellectualproperty/Trademarks/EN-US.aspx" data-bi-name="trademarks">商标</a></li>
		<li>© Microsoft 2021</li>
    </ul>
</footer>
	</div>

		<script type="text/x-mathjax-config;executed=true">
			MathJax.Hub.Config({
				showMathMenu: false,
				showMathMenuMSIE: false,
				tex2jax: {
					inlineMath: [['$','$'], ['\\(','\\)']],
					processEscapes: true
				}
			});
			MathJax.Hub.Register.StartupHook("End", function () {
				document.documentElement.classList.add('mathjax-complete');
			});
		</script>
		<script src="./如何使用 C# 对 JSON 进行序列化和反序列化 - .NET _ Microsoft Docs_files/MathJax.js.下载" integrity="sha384-RLsAi7wiVvnmHrRjlVvUjU45C0bfsIBg6Gsxue6azjJi+lA5q5uK+XbGzO3yn5g1" crossorigin="anonymous"></script>
	<div id="action-panel" role="region" aria-label="操作面板" class="action-panel has-default-focus" tabindex="-1"></div>


<div class="usabilla_live_button_container" role="button" tabindex="0" style="margin-top: 0px; position: fixed; width: 0px; height: 0px; z-index: 99999990; right: 0px; top: 50%; display: none;" aria-label="Usabilla Feedback Button"><iframe src="./如何使用 C# 对 JSON 进行序列化和反序列化 - .NET _ Microsoft Docs_files/saved_resource(1).html" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" data-tags="right" title="Usabilla Feedback Button" style="width: 0px; height: 0px; margin: 0px; padding: 0px; border: 0px; overflow: hidden; z-index: 9998; position: absolute; left: 0px; top: 0px; box-shadow: 0px 0px 0px; background-color: transparent;"></iframe></div></body></html>